Android应用程序到动态功能的链接显示应用程序选择器对话框,显示两次相同的应用程序

时间:2019-02-05 15:02:44

标签: android android-intent applinks dynamic-feature

我正在尝试使用Android应用链接打开活动。活动位于Google's Sample project的动态功能模块中。

我尚未将项目上传到Google Play,因此我正在使用调试构建类型进行测试,其运行配置包括APK的所有动态功能。

我要测试的code是:

 private fun openUrl(url: String) { // url will be "http://uabsample-405d6.firebaseapp.com/url"
    var intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    intent.setPackage(packageName)
    intent.addCategory(Intent.CATEGORY_BROWSABLE)
    startActivity(intent)
}

但是当我尝试导航到URL功能时,Android会显示两次选择同一应用程序的应用程序选择器对话框:

result

您知道为什么会这样吗?这是预期的行为吗?

Android Manifest of Url Module

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.android.samples.instantdynamicfeatures">

    <dist:module
        dist:instant="true"
        dist:onDemand="false"
        dist:title="@string/title_url_instant_module">
        <dist:fusing dist:include="true" />
    </dist:module>

    <application>
        <activity
            android:name="com.android.samples.instantdynamicfeatures.UrlInstantModuleActivity"
            android:label="@string/title_activity_url_instant_module"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter
                android:autoVerify="true"
                android:order="1">
                <action android:name="android.intent.action.VIEW" />

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

                <data
                    android:host="uabsample-405d6.firebaseapp.com"
                    android:path="/url"
                    android:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Strings

<resources>
    ...
    <string name="title_url_instant_module">url</string>
    <string name="title_activity_url_instant_module">url_instant_module_activity</string>
</resources>

更新: 我忘了提及:我用我的示例项目的应用程序ID进行了更改,并将知名的Json放在了我的网站上。我使用App Links Assistant进行了检查,很好。

2 个答案:

答案 0 :(得分:0)

这似乎是动态功能中的一个错误,该错误已报告here。使用URI意图。对于Google示例仓库,该错误已报告here,这是我目前只能提供的唯一解决方案是,暂时我们可以使用反射来完成这项工作。

  private fun openUrl(url: String) {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        intent.setPackage(BuildConfig.APPLICATION_ID)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.setClassName(getPackageName(),"com.android.samples.instantdynamicfeatures.UrlInstantModuleActivity")
        intent.addCategory(Intent.CATEGORY_BROWSABLE)
        startActivity(intent)
    }

setClassName 函数中使用应用程序包名称活动类名称。但这不是官方推荐的方法。有关更多信息,请检查this

答案 1 :(得分:0)

有一种可能的解决方案是将 URI 意图与动态功能结合使用。您可以使用一个技巧,在运行时使用 packageManager.queryIntentActivities() 获取所需的类名,这样您就不必使用硬编码的 Activity 名称。

以下扩展函数是如何将使用 URI 或深层链接的 Intent 转换为不显示选择器对话框的示例:

fun Intent.convertToSafeDynamicFeatureModuleIntent(context: Context) {
    //Get list of all intent handlers for this Intent. This should only be the actual activity we are looking for
    val options = context.packageManager.queryIntentActivities(this, PackageManager.MATCH_DEFAULT_ONLY)
    //Set the activity that supported the given intent
    setClassName(packageName, options[0].activityInfo.name)
}

然后你可以简单地做:

intent.convertToSafeDynamicFeatureModuleIntent(context)
startActivity(intent)

可以找到更长的解释here