android - 如何避免以编程方式在Android上打开其他Android商店并仅打开Goog​​le Play商店?

标签 android android-intent google-play

在很多小米手机中,当我用这个代码打开谷歌play商店时,小米商店就打开了。

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
如何避免这种情况并检查是否安装了 Google Play 商店并仅打开 Google Play 商店?

最佳答案

您可以设置 Intent的包裹到com.android.vending ,这是 Play 商店应用程序的包名称。这将使得只有该应用程序会收到 Intent。
基于 Linking to Google Play :
Kotlin :

val appPackageName = "your.package.name.here"
val intent = Intent(Intent.ACTION_VIEW).apply {
    data = Uri.parse(
            "https://play.google.com/store/apps/details?id=$appPackageName")
    setPackage("com.android.vending")
}
startActivity(intent)
java :
String appPackageName = "your.package.name.here";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
        "https://play.google.com/store/apps/details?id=" + appPackageName));
intent.setPackage("com.android.vending");
startActivity(intent);
如果未安装 Play 商店,则 startActivity调用将抛出 ActivityNotFoundException ,你可以捕获。
我相信这也适用于 market:// URLs,但我没有测试过,因为我没有小米手机来确认。

关于android - 如何避免以编程方式在Android上打开其他Android商店并仅打开Goog​​le Play商店?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63442430/

相关文章:

Google Play 商店中的 Android 应用与 LG G3(密度 538,尺寸 2560x1440)不兼容?

android - ConstraintLayout 中出现额外的边距

java - 使用 Android 模式和匹配器类(正则表达式)

android - Kotlin : pass data from adapter to activity

java - 我希望我的 Android 应用程序能够打开所有文件扩展名

android - 通过 google-play 帐户识别用户

android - 这个 Google Play APK 发布错误消息是什么意思?

android - Android 中的位图

java - 绑定(bind) Spinner 后,如何不选择第一项?

android - 使用 relativelayout 对齐在中心对齐的 2 行控件