首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从我的Android应用程序直接打开Google Play商店?

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

Stack Overflow用户
提问于 2012-08-01 13:27:10
回答 22查看 837K关注 0票数 642

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

代码语言:javascript
运行
复制
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 Store中打开应用程序。

EN

回答 22

Stack Overflow用户

回答已采纳

发布于 2012-08-01 13:34:23

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

Java

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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 Store,则将引发。

注意:任何应用程序都可以注册为能够处理market://details?id= Uri,如果您想特别针对Google Play,请查看Berťák答案

票数 1.6K
EN

Stack Overflow用户

发布于 2015-01-22 22:15:54

这里的许多答案建议使用Uri.parse("market://details?id=" + appPackageName))打开Google Play,但我认为这是不够的事实上:

一些第三方应用程序可以使用自己的意图过滤器"market://"定义的方案,因此他们可以处理提供的Uri,而不是Google Play (我在e.g.SnapPea应用程序中遇到过这种情况)。问题是“如何打开Google Play商店?”所以我假设你不想打开任何其他应用程序。还请注意,例如,应用程序评级只在GP Store应用程序等相关…

要打开Google Play并且只打开Google Play,我使用以下方法:

代码语言:javascript
运行
复制
public static void openAppRating(Context context) {
    // you can also use BuildConfig.APPLICATION_ID
    String appId = context.getPackageName();
    Intent rateIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("market://details?id=" + appId));
    boolean marketFound = false;

    // find all applications able to handle our rateIntent
    final List otherApps = context.getPackageManager()
        .queryIntentActivities(rateIntent, 0);
    for (ResolveInfo otherApp: otherApps) {
        // look for Google Play application
        if (otherApp.activityInfo.applicationInfo.packageName
                .equals("com.android.vending")) {

            ActivityInfo otherAppActivity = otherApp.activityInfo;
            ComponentName componentName = new ComponentName(
                    otherAppActivity.applicationInfo.packageName,
                    otherAppActivity.name
                    );
            // make sure it does NOT open in the stack of your activity
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // task reparenting if needed
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            // if the Google Play was already open in a search result
            //  this make sure it still go to the app page you requested
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // this make sure only the Google Play app is allowed to
            // intercept the intent
            rateIntent.setComponent(componentName);
            context.startActivity(rateIntent);
            marketFound = true;
            break;

        }
    }

    // if GP not present on device, open web browser
    if (!marketFound) {
        Intent webIntent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://play.google.com/store/apps/details?id="+appId));
        context.startActivity(webIntent);
    }
}

重点是,当Google Play之外的更多应用程序可以打开我们的意图时,应用程序选择器对话框被跳过,GP应用程序直接启动。

更新:

有时它似乎只打开GP应用程序,而不打开应用程序的配置文件。正如TrevorWiley在他的评论中所建议的那样,Intent.FLAG_ACTIVITY_CLEAR_TOP可以解决这个问题。(我还没有亲自测试过...)

请参见这个答案为了理解Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED确实如此。

票数 180
EN

Stack Overflow用户

发布于 2014-11-22 17:36:32

去Android开发者官方链接作为教程,一步一步地看,并从play store获得你的应用程序包的代码,如果存在或play store应用程序不存在,则从web浏览器打开应用程序。

Android开发者官方链接

https://developer.android.com/distribute/tools/promote/linking.html

链接到应用程序页面

来自网站:https://play.google.com/store/apps/details?id=

来自Android应用程序:market://details?id=

链接到产品列表

来自网站:https://play.google.com/store/search?q=pub:

来自Android应用程序:market://search?q=pub:

链接到搜索结果

来自网站:https://play.google.com/store/search?q=&c=apps

来自Android应用程序:market://search?q=&c=apps

票数 91
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11753000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档