【问题标题】:Android App Link to Dynamic Feature Shows the Application Chooser Dialog Showing Same App TwiceAndroid 应用程序链接到动态功能显示应用程序选择器对话框两次显示相同的应用程序
【发布时间】:2019-02-05 15:02:43
【问题描述】:

我正在尝试使用 Android 应用链接打开一个 Activity。 Activity 位于动态功能模块中,位于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 会显示应用程序选择器对话框,两次显示相同的应用程序:

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

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 进行了检查,一切正常。

【问题讨论】:

  • 我认为你不需要setPackage()addCategory() 来表达意图。试试这个!
  • 这个问题有什么解决办法吗?
  • 很遗憾没有

标签: android android-intent applinks dynamic-feature


【解决方案1】:

这似乎是使用 URI 意图的动态功能中的一个错误,已报告 here。对于 google 示例 repo,报告了该错误 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

【讨论】:

    【解决方案2】:

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

    以下扩展函数是一个示例,说明如何使用 URI 或深层链接将 Intent 转换为不会显示选择器对话框的 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

    【讨论】:

      猜你喜欢
      • 2015-04-16
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多