如何使用android中的深层链接启动应用程序

时间:2014-09-08 10:18:47

标签: android deep-linking

我想使用自己的应用启动应用,但不是通过提供包名称,我想打开自定义网址。

我这样做是为了启动一个应用程序。

Intent intent = getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
startActivity(intent);

代替包名称可以提供深层链接,例如:

"mobiledeeplinkingprojectdemo://product/123"

Reference

2 个答案:

答案 0 :(得分:13)

您需要定义一个订阅所需意图过滤器的活动:

<activity
            android:name="DeepLinkListener"
            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:host="host"
                    android:pathPattern="some regex"
                    android:scheme="scheme" />
            </intent-filter>
        </activity>

然后在您的DeepLinkListener活动的onCreate中,您可以访问主机,方案等:

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
Intent deepLinkingIntent= getIntent();
deepLinkingIntent.getScheme();
deepLinkingIntent.getData().getPath();
}

执行路径检查并再次触发意图以使用户进行相应的活动。 请参考data获取更多帮助

现在激发意图:

Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData (Uri.parse(DEEP_LINK_URL));

答案 1 :(得分:0)

不要忘记处理异常。如果没有可以处理深层链接的活动,startActivity 将返回异常。

    try {
    context.startActivity(
        Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse(deepLink)
        }
    )
} catch (exception: Exception) {
    Toast.makeText(context, exception.localizedMessage, Toast.LENGTH_LONG).show()
}
相关问题