上篇文章我们讲了如何使用deepLink来唤起App,详情见https://community.jiguang.cn/article/464301 ,这周我们介绍下如何使用deepLink进行传参,跳转到目标页面。
使用deepLink传递参数并跳转至目标页面:
1.配置scheme;这里我们使用test;//host
2.android端配置manifest

<activity android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="test" /> // 注意这里就是上面设置的scheme
            </intent-filter>
        </activity>

3.android端接收参数并跳转至目标页面
注意在scheme对应的页面中编写下面代码,本例中是MainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Uri uri = getIntent().getData();
        if (uri != null) {
            String type = uri.getQueryParameter("type");
            String id = uri.getQueryParameter("id");
            if (type != null && type.equals("goods")) {
                Intent intent = new Intent(getApplicationContext(), GoodsActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                finish();
            }
        }
    }

4.编写测试页面

<html>
<a href="test://host?type=goods&id=111">点我唤起app</a>
</html>

分析:
在h5里我们设置了一个链接test://host?type=goods&id=111,这个uri会传递到app中scheme对应的activity,使用getIntent().getData()可以取出test://host?type=goods&id=111,然后再对uri进行解析取出参数,跳转至目标页面