【问题标题】:How to launch an app using a deeplink in android如何在 android 中使用深层链接启动应用程序
【发布时间】:2014-11-01 12:57:34
【问题描述】:

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

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

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

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

"mobiledeeplinkingprojectdemo://product/123"

Reference

【问题讨论】:

    标签: android deep-linking


    【解决方案1】:

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

    <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 intent = new Intent (Intent.ACTION_VIEW);
    intent.setData (Uri.parse(DEEP_LINK_URL));
    

    【讨论】:

    • 嘿,谢谢,但是有没有办法在 xml 文件中不指定任何内容以及这个 deepLinkingIntent 是什么?
    • 如果您知道要启动的活动,那么为什么不直接通过 Intent 启动它呢?
    • 这个答案是针对问题的反面。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多