如何使用Navigation Component启动Android端Play Store应用
结论
完全可以通过Navigation Components实现拉起Google Play Store的需求,你运行时抛出ActivityNotFoundException是因为配置存在认知错误和参数错误。
配置错误说明
- 对
标签的作用理解错误:导航图中的 是入站规则配置项,作用是声明当外部触发符合规则的Intent时,跳转到你App内的当前目的地,不是用来构造跳转到外部App的出站Intent参数,你完全用错了标签的适用场景。 - Action常量值拼写错误:系统
ACTION_VIEW的正确值为android.intent.action.VIEW,你配置的android.intent.action.ACTION_VIEW多了冗余的ACTION_前缀,系统无法识别该action。 - 属性使用错误:
标签本身不支持 app:targetPackage属性,你配置的包名参数不会被解析到最终构造的Intent中,Intent无法匹配到Google Play的对应Activity。 - 目标URI不合法:你配置的
https://play.google.com/store/apps?someapp不是Google Play合法的应用页地址,正常跳转到指定应用的地址格式为https://play.google.com/store/apps/details?id=目标应用包名。
正确实现步骤
1. 修改导航图配置
移除错误的
<!-- 固定跳转目标的配置 --> <activity android:id="@+id/nav_google_play" app:action="android.intent.action.VIEW" app:data="https://play.google.com/store/apps/details?id=固定要跳转的应用包名" app:targetPackage="com.android.vending" />
如果需要动态传入要跳转的应用ID,可以添加参数占位符:
<activity android:id="@+id/nav_google_play" app:action="android.intent.action.VIEW" app:data="https://play.google.com/store/apps/details?id={appId}" app:targetPackage="com.android.vending"> <argument android:name="appId" app:argType="string" /> </activity>
2. 触发跳转
在按钮点击事件中通过NavController执行导航即可,动态传参场景传入对应参数:
// 固定跳转目标写法 findNavController().navigate(R.id.nav_google_play); // 动态传参写法,示例为跳转到谷歌地图 Bundle args = new Bundle(); args.putString("appId", "com.google.android.apps.maps"); findNavController().navigate(R.id.nav_google_play, args);
兼容注意事项
- 部分设备未预装Google Play Store,跳转前建议先调用
Intent.resolveActivity()检测是否存在可处理该Intent的应用,避免触发崩溃。 - 检测到无Google Play时,可以选择移除跳转入口,或者构造不带包名限制的Intent跳转到系统浏览器打开Google Play网页端。
内容的提问来源于stack exchange,提问作者mynameisteodora
相关产品推荐
相关产品推荐

