如何直接从我的 Android 应用程序打开 Google Play 商店? [英] How to open the Google Play Store directly from my Android application?

查看:108
本文介绍了如何直接从我的 Android 应用程序打开 Google Play 商店?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have open the Google Play store using the following code

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);.

但它向我展示了一个完整的动作视图来选择选项(浏览器/游戏商店).我需要直接在 Play 商店中打开应用程序.

But it shows me a Complete Action View as to select the option (browser/play store). I need to open the application in Play Store directly.

推荐答案

您可以使用 market:// 前缀.

You can do this using the market:// prefix.

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)));
}

科特林

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 块是因为如果目标设备上没有安装 Play 商店,将会抛出一个Exception.

We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.

注意:任何应用都可以注册为能够处理 market://details?id=<appId> Uri,如果您想专门针对 Google Play检查 Berťák 的答案

NOTE: any app can register as capable of handling the market://details?id=<appId> Uri, if you want to specifically target Google Play check the Berťák answer

这篇关于如何直接从我的 Android 应用程序打开 Google Play 商店?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆