关于APP唤醒的方法总结

对于一些比较大型的项目,业务中可能会有其他APP的入口。一般分为两种:1.项目中内嵌一个友商的SDK(集成了部分业务的SDK)    2.唤醒友商的SDK(主要是已经安装的app),今天主要说下唤醒吧。

  1.使用Intent,通过包名.类型进行唤醒:

  被唤起的APP的类在manifest.xml的配置:

<activity android:name=".MyTestActivity1"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

 唤醒启动:com.example.myapplication1为包名,com.example.myapplication1.MyTestActivity1 包名.类名

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.myapplication1",
        "com.example.myapplication1.MyTestActivity1"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

2.通过intent使用Uri唤醒:

<activity android:name=".TestActivity3"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="test.action" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:host="test.uri.activity"
            android:scheme="app" />
    </intent-filter>
</activity>

启动:

Uri uri = Uri.parse("app://test.uri.activity?id=1");
Intent intent2 = new Intent("test.action");
intent2.setData(uri);
startActivity(intent2);

被唤醒的页面获取Uri传递过来的参数:

if (null != intent) {
    Uri uri = intent.getData();
    if (uri == null) {
        return;
    }
    String TAG = "TAG";
    if (uri != null) {
        // 完整的url信息
        String url = uri.toString();
        Log.e(TAG, "url: " + url);
        // scheme部分
        String scheme = uri.getScheme();
        Log.e(TAG, "scheme: " + scheme);
        // host部分
        String host = uri.getHost();
        Log.e(TAG, "host: " + host);
        String data = uri.getQueryParameter("id");
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }

}

3.H5网页浏览器唤醒:

1.先写一个web小项目,有网页就行如下图所示,并且部署到tomcat:

在浏览器上打开此链接便是:

咱们肯定是在手机的浏览器中打开此链接,也是这个样子的

2.配置:

<activity android:name=".TestActivity4"
    android:exported="true"
    >
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="app" android:host="test.my.app" android:pathPrefix="/path" />
    </intent-filter>
</activity>

配置完毕之后,点击手机浏览器加载的此网页的“”点我吧...“”既可以唤醒了。

对应的此页面获取参数:

 String TAG="TAG";
        Uri uri = getIntent().getData();
        if (uri != null) {
            // 完整的url信息
            String url = uri.toString();
            Log.e(TAG, "url: " + url);
            // scheme部分
            String scheme = uri.getScheme();
            Log.e(TAG, "scheme: " + scheme);
            // host部分
            String host = uri.getHost();
            Log.e(TAG, "host: " + host);
            //port部分
//            int port = uri.getPort();
//            Log.e(TAG, "host: " + port);
            // 访问路劲
            String path = uri.getPath();
            Log.e(TAG, "path: " + path);
            List<String> pathSegments = uri.getPathSegments();
            // Query部分
            String query = uri.getQuery();
            Log.e(TAG, "query: " + query);
            //获取指定参数值
            String goodsId = uri.getQueryParameter("query1");
            Log.e(TAG, "query1: " + goodsId)

注意:1.scheme,host,path都小写

            2.scheme不要用http或者https

猜你喜欢

转载自blog.csdn.net/lk2021991/article/details/96006211
今日推荐