android - 如何直接从我的 Android 应用程序打开 Google Play 商店?

标签 android android-intent google-play

我已经使用以下代码打开了 Google Play 商店

Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename "));
startActivity(i);.

但它显示了一个完整的操作 View 来选择选项(浏览器/播放商店)。我需要直接在 Play Store 中打开应用程序。

最佳答案

您可以使用 market:// prefix 执行此操作.

Java

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

Kotlin

try {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
} catch (e: ActivityNotFoundException) {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName")))
}

我们使用 try/catch在这里阻止,因为 Exception如果目标设备上未安装 Play 商店,则会抛出此错误。

注意:任何应用程序都可以注册为能够处理 market://details?id=<appId> URI。如果您想专门针对 Google Play,Berťák's answer 中的解决方案是一个不错的选择。

关于android - 如何直接从我的 Android 应用程序打开 Google Play 商店?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753000/

相关文章:

android - 无法生成发布构建 apk

c# - 画图 : Canvas is not drawing bitmap

android - 无法从 Google 相册应用中选取图片

android - 为什么Google Play商店允许人们在我的生产版本中使用测试卡?

java - 如何将值从父 Activity 传递到子 Activity 以及在子 Activity 退出时刷新父 Activity

android - 如何将 PendingIntent 保存到 SQLite 数据库?

android - 在禁用市场的情况下使用 Android 的 C2DM

Android:是否可以在 Activity 和服务之间共享 GoogleApiClient?

android - Android 的 XML 属性中的问号 (?)

java - android studio 无法在 ImageView 中显示图像