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

Raj*_*mar 534 android android-intent google-play

我已使用以下代码打开Goog​​le 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);.
Run Code Online (Sandbox Code Playgroud)

但它显示了一个完整的动作视图,以选择选项(浏览器/播放商店).我需要直接在Play商店中打开应用程序.

Eri*_*ric 1364

您可以使用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)));
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果你想重定向到所有开发者的应用程序,请使用`market:// search?q = pub:"+ devName`和`http://play.google.com/store/search?q=pub:"+ devName` (48认同)
  • 好建议,但请记住"appName"是包名 (18认同)
  • 对于使用Gradle构建系统的项目,`appPackageName`实际上是`BuildConfig.APPLICATION_ID`.没有`Context` /`Activity`依赖,减少了内存泄漏的风险. (17认同)
  • 如果某些应用程序使用定义了"market://"方案的intent过滤器,则此解决方案不起作用.请参阅我的回答如何打开Goog​​le Play和仅限Google Play应用程序(如果GP不存在,则为webbrowser).:-) (3认同)
  • 您仍然需要上下文来启动intent.Context.startActivity() (2认同)
  • 此解决方案假设有打开 Web 浏览器的意图。这并不总是正确的(就像在 Android TV 上一样)所以要小心。您可能需要使用 intent.resolveActivity(getPackageManager()) 来确定要执行的操作。 (2认同)

Ber*_*ťák 154

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

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

要打开Goog​​le Play并且只打开Goog​​le 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

更新: 有时它似乎只打开GP应用程序,而无需打开应用程序的配置文件.正如TrevorWiley在评论中所说,Intent.FLAG_ACTIVITY_CLEAR_TOP可以解决问题.(我自己还没试过......)

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

  • 虽然这很好,但对于当前的Google Play版本来说似乎也不可靠,如果您在Google Play上输入其他应用页面然后触发此代码,它只会打开Goog​​le Play但不会转到您的应用. (4认同)
  • @ dum4ll3我想你可以,但是这段代码也隐含地检查是否安装了Google Play应用.如果你不检查它,你需要捕获ActivityNotFound (3认同)
  • @zoltish,我已将Intent.FLAG_ACTIVITY_CLEAR_TOP添加到标志中,这似乎可以解决问题 (2认同)
  • 如果您使用`rateIntent.setPackage("com.android.vending")`以确保PlayStore应用程序将处理此意图,而不是所有这些代码,会发生什么? (2认同)

Naj*_*ala 74

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

Android开发者官方链接

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

链接到应用程序页面

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

来自Android应用: market://details?id=<package_name>

链接到产品列表

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

来自Android应用: market://search?q=pub:<publisher_name>

链接到搜索结果

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

来自Android应用: market://search?q=<seach_query>&c=apps

  • @loki我认为重点是它不再被列为建议。如果您在该页面搜索“市场”一词,您将找不到任何解决方案。我认为新方法是激发更通用的意图 https://developer.android.com/distribute/marketing-tools/linking-to-google-play#android-app 。Play 商店应用程序的最新版本可能有一个针对此 URI `https://play.google.com/store/apps/details?id=com.example.android` 的意图过滤器 (2认同)

You*_*ddh 24

试试这个

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


Ryu*_*yuk 19

如果您确实想要独立打开Goog​​le Play(或任何其他应用),以上所有答案都会在相同应用的新视图中打开Goog​​le 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);
Run Code Online (Sandbox Code Playgroud)

重要的是,实际上是独立打开谷歌播放或任何其他应用程序.

我所看到的大部分内容都使用了其他答案的方法,而这并不是我所需要的,这有助于某人.

问候.

  • 我的观点很简单,这段代码并不是人们可以简单地移植到自己的应用程序中使用的东西.削减脂肪并只留下核心方法对未来的读者有用. (3认同)

Pao*_*lli 13

您可以检查是否已安装Google Play商店应用,如果是这种情况,您可以使用"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);
Run Code Online (Sandbox Code Playgroud)


M3-*_*n50 12

虽然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)));
}
Run Code Online (Sandbox Code Playgroud)

通过使用setPackage,您可以强制设备使用Play商店.如果没有安装Play商店,Exception则会被捕获.


Joh*_*lin 11

使用市场://

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


小智 8

科特林:

延期:

fun Activity.openAppInGooglePlay(){

val appId = BuildConfig.APPLICATION_ID
try {
    this.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
} catch (anfe: ActivityNotFoundException) {
    this.startActivity(
        Intent(
            Intent.ACTION_VIEW,
            Uri.parse("https://play.google.com/store/apps/details?id=$appId")
        )
    )
}}
Run Code Online (Sandbox Code Playgroud)

方法:

    fun openAppInGooglePlay(activity:Activity){

        val appId = BuildConfig.APPLICATION_ID
        try {
            activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appId")))
        } catch (anfe: ActivityNotFoundException) {
            activity.startActivity(
                Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=$appId")
                )
            )
        }
    }
Run Code Online (Sandbox Code Playgroud)


alm*_*awi 7

你可以做:

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

在这里得到参考:

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


app*_*oll 7

这个问题的一些答案已经过时了。

根据此链接,对我有用的(在 2020 年)是明确告诉意图跳过选择器并直接打开 Play 商店应用程序:

“如果您想从 Android 应用链接到您的产品,请创建一个打开 URL 的 Intent。在配置此 Intent 时,将“com.android.vending”传递到 Intent.setPackage() 中,以便用户在Google Play 商店应用程序而不是选择器。”

这是我用来引导用户在 Google Play 中查看包含包名称 com.google.android.apps.maps 的应用程序的 Kotlin 代码:

val intent = Intent(Intent.ACTION_VIEW).apply {
               data = Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")
               setPackage("com.android.vending")
            }
            startActivity(intent)
Run Code Online (Sandbox Code Playgroud)

我希望能帮助别人!


Hus*_*sim 6

派对晚了,官方文档在这里。和描述的代码是

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);
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

如果您已经使用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);
Run Code Online (Sandbox Code Playgroud)

对于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)
Run Code Online (Sandbox Code Playgroud)


Ale*_*ndr 5

即用型解决方案:

public class GoogleServicesUtils {

    public static void openAppInGooglePlay(Context context) {
        final String appPackageName = context.getPackageName();
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        } catch (android.content.ActivityNotFoundException e) { // if there is no Google Play on device
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

基于Eric的答案。


Tah*_*mid 5

如果您想从您的应用程序打开 Google Play 商店,请直接使用此命令:market://details?gotohome=com.yourAppName,它将打开您的应用程序的 Google Play 商店页面。

显示特定发布商的所有应用

搜索在标题或描述上使用查询的应用程序

参考: https: //tricklio.com/market-details-gotohome-1/


Nik*_*rov 5

market://如果您使用的是 Android 设备,则此链接将在浏览器中自动打开该应用程序,如果您使用的是 PC。

https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
Run Code Online (Sandbox Code Playgroud)


Khe*_*raj 5

科特林

fun openAppInPlayStore(appPackageName: String) {
    try {
        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$appPackageName")))
    } catch (exception: android.content.ActivityNotFoundException) {
        startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")))
    }
}
Run Code Online (Sandbox Code Playgroud)


ser*_*inc 5

由于官方文档使用https://代替market://,因此将Eric和M3-n50的答案与代码重用结合在一起(不要重复自己):

Intent intent = new Intent(Intent.ACTION_VIEW)
    .setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
try {
    startActivity(new Intent(intent)
                  .setPackage("com.android.vending"));
} catch (android.content.ActivityNotFoundException exception) {
    startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

如果存在,它将尝试使用GPlay应用打开,并恢复为默认状态。