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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11753000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:43:47  来源:igfitidea点击:

How to open the Google Play Store directly from my Android application?

androidandroid-intentgoogle-play

提问by Rajesh Kumar

I have open the Google Play store using the following code

我已使用以下代码打开 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);.

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.

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

回答by Eric

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

您可以使用market://前缀来执行此操作。

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

We use a try/catchblock here because an Exceptionwill be thrown if the Play Store is not installed on the target device.

我们try/catch在这里使用一个块是因为Exception如果目标设备上没有安装 Play 商店,将会抛出一个。

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?ákanswer

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

回答by Ber?ák

Many answers here suggest to useUri.parse("market://details?id=" + appPackageName))to open Google Play, but I think it is insufficientin fact:

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

Some third-party applications can use its own intent-filters with "market://"scheme defined, thus they can process supplied Uri instead of Google Play (I experienced this situation with e.g.SnapPea application). The question is "How to open the Google Play Store?", so I assume, that you do not want to open any other application. Please also note, that e.g. app rating is only relevant in GP Store app etc...

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

To open Google Play AND ONLY Google Play I use this method:

要打开 Google Play AND ONLY Google Play 我使用这个方法:

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

The point is that when more applications beside Google Play can open our intent, app-chooser dialog is skipped and GP app is started directly.

关键是当 Google Play 旁边的更多应用程序可以打开我们的 Intent 时,会跳过 app-chooser 对话框并直接启动 GP 应用程序。

UPDATE:Sometimes it seems that it opens GP app only, without opening the app's profile. As TrevorWiley suggested in his comment, Intent.FLAG_ACTIVITY_CLEAR_TOPcould fix the problem. (I didn't test it myself yet...)

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

See this answerfor understanding what Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDEDdoes.

请参阅此答案以了解其Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED作用。

回答by Najib Ahmed Puthawala

Go on Android Developer official link as tutorial step by step see and got the code for your application package from play store if exists or play store apps not exists then open application from web browser.

继续 Android Developer 官方链接作为教程逐步查看并从 Play 商店获取应用程序包的代码(如果存在)或 Play 商店应用程序不存在,然后从 Web 浏览器打开应用程序。

Android Developer official link

Android 开发者官方链接

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

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

Linking to a Application Page

链接到应用程序页面

From a web site: https://play.google.com/store/apps/details?id=<package_name>

从一个网站: https://play.google.com/store/apps/details?id=<package_name>

From an Android app: market://details?id=<package_name>

从 Android 应用程序: market://details?id=<package_name>

Linking to a Product List

链接到产品列表

From a web site: https://play.google.com/store/search?q=pub:<publisher_name>

从一个网站: https://play.google.com/store/search?q=pub:<publisher_name>

From an Android app: market://search?q=pub:<publisher_name>

从 Android 应用程序: market://search?q=pub:<publisher_name>

Linking to a Search Result

链接到搜索结果

From a web site: https://play.google.com/store/search?q=<search_query>&c=apps

从一个网站: https://play.google.com/store/search?q=<search_query>&c=apps

From an Android app: market://search?q=<seach_query>&c=apps

从 Android 应用程序: market://search?q=<seach_query>&c=apps

回答by Youddh

try this

尝试这个

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);

回答by code4jhon

All of the above answers open Google Play in a new view of the same app, if you actually want to open Google Play (or any other app) independently:

如果您确实想独立打开 Google Play(或任何其他应用程序),以上所有答案都会在同一应用程序的新视图中打开 Google Play:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");

// package name and activity
ComponentName comp = new ComponentName("com.android.vending",
                                       "com.google.android.finsky.activities.LaunchUrlHandlerActivity"); 
launchIntent.setComponent(comp);

// sample to open facebook app
launchIntent.setData(Uri.parse("market://details?id=com.facebook.katana"));
startActivity(launchIntent);

The important part is that actually opens google play or any other app independently.

重要的部分是实际独立打开 google play 或任何其他应用程序。

Most of what I have seen uses the approach of the other answers and it was not what I needed hopefully this helps somebody.

我所看到的大部分内容都使用了其他答案的方法,这不是我所需要的,希望这对某人有所帮助。

Regards.

问候。

回答by Paolo Rovelli

You can check if the Google Play Storeapp is installed and, if this is the case, you can use the "market://"protocol.

您可以检查是否安装了Google Play Store应用程序,如果是这种情况,您可以使用“market://”协议。

final String my_package_name = "........."  // <- HERE YOUR PACKAGE NAME!!
String url = "";

try {
    //Check whether Google Play store is installed or not:
    this.getPackageManager().getPackageInfo("com.android.vending", 0);

    url = "market://details?id=" + my_package_name;
} catch ( final Exception e ) {
    url = "https://play.google.com/store/apps/details?id=" + my_package_name;
}


//Open the app page in Google Play store:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(intent);

回答by M3-n50

While Eric's answer is correct and Ber?ák's code also works. I think this combines both more elegantly.

虽然 Eric 的回答是正确的,而且 Ber?ák 的代码也有效。我认为这将两者结合得更优雅。

try {
    Intent appStoreIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
    appStoreIntent.setPackage("com.android.vending");

    startActivity(appStoreIntent);
} catch (android.content.ActivityNotFoundException exception) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

By using setPackage, you force the device to use the Play Store. If there is no Play Store installed, the Exceptionwill be caught.

通过使用setPackage,您可以强制设备使用 Play 商店。如果没有安装Play Store,Exception就会被抓到。

回答by Johannes Staehlin

use market://

使用市场://

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));

回答by almalkawi

You can do:

你可以做:

final Uri marketUri = Uri.parse("market://details?id=" + packageName);
startActivity(new Intent(Intent.ACTION_VIEW, marketUri));

get Reference here:

在此处获取参考:

You can also try the approach described in the accepted answer of this question: Cannot determine whether Google play store is installed or not on Android device

您还可以尝试此问题的已接受答案中描述的方法: 无法确定 Android 设备上是否安装了 Google Play 商店

回答by Husnain Qasim

Very late in the party Official docsare here. And code described is

晚会官方文档在这里。描述的代码是

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
    "https://play.google.com/store/apps/details?id=com.example.android"));
intent.setPackage("com.android.vending");
startActivity(intent);

As you configure this intent, pass "com.android.vending"into Intent.setPackage()so that users see your app's details in the Google Play Store appinstead of a chooser. for KOTLIN

当你配置此意图,传递"com.android.vending"Intent.setPackage()让用户看到你的应用程序的详细信息,谷歌Play商店的应用程序,而不是一个选择器。科特林

val intent = Intent(Intent.ACTION_VIEW).apply {
    data = Uri.parse(
            "https://play.google.com/store/apps/details?id=com.example.android")
    setPackage("com.android.vending")
}
startActivity(intent)

If you have published an instant app using Google Play Instant, you can launch the app as follows:

如果您已使用 Google Play Instant 发布了免安装应用,则可以按如下方式启动该应用:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri.Builder uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
    .buildUpon()
    .appendQueryParameter("id", "com.example.android")
    .appendQueryParameter("launch", "true");

// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using
// Activity.getIntent().getData().
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId");

intent.setData(uriBuilder.build());
intent.setPackage("com.android.vending");
startActivity(intent);

For KOTLIN

对于科特林

val uriBuilder = Uri.parse("https://play.google.com/store/apps/details")
        .buildUpon()
        .appendQueryParameter("id", "com.example.android")
        .appendQueryParameter("launch", "true")

// Optional parameters, such as referrer, are passed onto the launched
// instant app. You can retrieve these parameters using Activity.intent.data.
uriBuilder.appendQueryParameter("referrer", "exampleCampaignId")

val intent = Intent(Intent.ACTION_VIEW).apply {
    data = uriBuilder.build()
    setPackage("com.android.vending")
}
startActivity(intent)