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

Kan*_*ngh 8 android deep-linking

我想使用自己的应用程序启动应用程序,但不是通过提供程序包名称,我想打开自定义URL.

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

Intent intent = getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

而不是包名称可以提供深层链接,例如:

"mobiledeeplinkingprojectdemo://product/123"
Run Code Online (Sandbox Code Playgroud)

参考

Gau*_*gla 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>
Run Code Online (Sandbox Code Playgroud)

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

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
Intent deepLinkingIntent= getIntent();
deepLinkingIntent.getScheme();
deepLinkingIntent.getData().getPath();
}
Run Code Online (Sandbox Code Playgroud)

对路径执行检查并再次触发将用户带到相应活动的意图.参照数据以获得更多帮助

现在点了一个意图:

Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData (Uri.parse(DEEP_LINK_URL));
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是针对问题的反面。 (2认同)

Caf*_*han 5

不要忘记处理异常。如果没有可以处理深层链接的 Activity,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()
}
Run Code Online (Sandbox Code Playgroud)