651

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

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

4

24 回答 24

1618

您可以使用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)));
}

科特林

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答案

于 2012-08-01T05:34:22.717 回答
181

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

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

要打开 Google Play 并且仅打开 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);
    }
}

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

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

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

于 2015-01-22T14:15:53.597 回答
92

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

Android 开发者官方链接

https://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

于 2014-11-22T09:36:32.343 回答
27

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

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

于 2017-05-23T16:05:53.347 回答
26

试试这个

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
于 2012-08-01T05:36:53.683 回答
23

如果您真的想独立打开 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);

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

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

问候。

于 2014-07-29T03:10:53.560 回答
15

您可以检查是否安装了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);
于 2014-04-10T10:18:08.657 回答
12

使用市场://

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + my_packagename));
于 2012-08-01T05:35:07.910 回答
9

由于官方文档使用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);
}

它会尝试使用 GPlay 应用程序打开(如果存在)并回退到默认值。

于 2019-06-25T06:52:41.460 回答
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")
        )
    )
}}

方法:

    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")
                )
            )
        }
    }
于 2019-07-10T01:11:56.467 回答
7

你可以做:

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

在此处获取参考:

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

于 2012-08-01T05:35:48.500 回答
7

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

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

当您配置此意图时,请传递"com.android.vending"Intent.setPackage(),以便用户在Google 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)

如果您使用 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);

对于科特林

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)
于 2018-10-17T20:31:20.137 回答
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)

我希望对某人有所帮助!

于 2020-07-14T20:51:21.483 回答
6

即用型解决方案:

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

}

根据埃里克的回答。

于 2016-07-11T20:11:22.073 回答
5

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

https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.app.id&ddl=1&pcampaignid=web_ddl_1
于 2018-09-10T07:28:01.627 回答
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")))
    }
}
于 2018-09-21T13:10:53.583 回答
4

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

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

  • 网站:http ://play.google.com/store/search?q=pub :
  • 应用程序:market://search?q=pub:

搜索在标题或描述上使用 Query 的应用

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

于 2016-12-24T07:26:46.750 回答
3

这是上面答案的最终代码,首先尝试使用 Google Play 商店应用程序打开应用程序,特别是 Play 商店,如果失败,它将使用网络版本启动操作视图:感谢 @Eric,@Jonathan Caballero

public void goToPlayStore() {
        String playStoreMarketUrl = "market://details?id=";
        String playStoreWebUrl = "https://play.google.com/store/apps/details?id=";
        String packageName = getActivity().getPackageName();
        try {
            Intent intent =  getActivity()
                            .getPackageManager()
                            .getLaunchIntentForPackage("com.android.vending");
            if (intent != null) {
                ComponentName androidComponent = new ComponentName("com.android.vending",
                        "com.google.android.finsky.activities.LaunchUrlHandlerActivity");
                intent.setComponent(androidComponent);
                intent.setData(Uri.parse(playStoreMarketUrl + packageName));
            } else {
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreMarketUrl + packageName));
            }
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(playStoreWebUrl + packageName));
            startActivity(intent);
        }
    }
于 2018-02-16T20:14:14.330 回答
2

我结合了BerťákStefano Munarini的答案来创建一个混合解决方案,该解决方案同时处理评价这个应用程序显示更多应用程序场景。

        /**
         * This method checks if GooglePlay is installed or not on the device and accordingly handle
         * Intents to view for rate App or Publisher's Profile
         *
         * @param showPublisherProfile pass true if you want to open Publisher Page else pass false to open APp page
         * @param publisherID          pass Dev ID if you have passed PublisherProfile true
         */
        public void openPlayStore(boolean showPublisherProfile, String publisherID) {

            //Error Handling
            if (publisherID == null || !publisherID.isEmpty()) {
                publisherID = "";
                //Log and continue
                Log.w("openPlayStore Method", "publisherID is invalid");
            }

            Intent openPlayStoreIntent;
            boolean isGooglePlayInstalled = false;

            if (showPublisherProfile) {
                //Open Publishers Profile on PlayStore
                openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("market://search?q=pub:" + publisherID));
            } else {
                //Open this App on PlayStore
                openPlayStoreIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=" + getPackageName()));
            }

            // find all applications who can handle openPlayStoreIntent
            final List<ResolveInfo> otherApps = getPackageManager()
                    .queryIntentActivities(openPlayStoreIntent, 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
                    openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    // task reparenting if needed
                    openPlayStoreIntent.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
                    openPlayStoreIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    // this make sure only the Google Play app is allowed to
                    // intercept the intent
                    openPlayStoreIntent.setComponent(componentName);
                    startActivity(openPlayStoreIntent);
                    isGooglePlayInstalled = true;
                    break;

                }
            }
            // if Google Play is not Installed on the device, open web browser
            if (!isGooglePlayInstalled) {

                Intent webIntent;
                if (showPublisherProfile) {
                    //Open Publishers Profile on web browser
                    webIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://play.google.com/store/search?q=pub:" + getPackageName()));
                } else {
                    //Open this App on web browser
                    webIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
                }
                startActivity(webIntent);
            }
        }

用法

  • 打开发布者资料
   @OnClick(R.id.ll_more_apps)
        public void showMoreApps() {
            openPlayStore(true, "Hitesh Sahu");
        }
  • 在 PlayStore 上打开应用程序页面
@OnClick(R.id.ll_rate_this_app)
public void openAppInPlayStore() {
    openPlayStore(false, "");
}
于 2017-08-17T06:13:37.363 回答
2
public void launchPlayStore(Context context, String packageName) {
    Intent intent = null;
    try {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse("market://details?id=" + packageName));
            context.startActivity(intent);
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)));
        }
    }
于 2017-09-05T05:10:53.053 回答
2

为此目的,我的 kotlin entension 函数

fun Context.canPerformIntent(intent: Intent): Boolean {
        val mgr = this.packageManager
        val list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        return list.size > 0
    }

在你的活动中

val uri = if (canPerformIntent(Intent(Intent.ACTION_VIEW, Uri.parse("market://")))) {
            Uri.parse("market://details?id=" + appPackageName)
        } else {
            Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)
        }
        startActivity(Intent(Intent.ACTION_VIEW, uri))
于 2018-02-01T10:31:22.277 回答
1

人民,不要忘记你实际上可以从中得到更多的东西。例如,我的意思是 UTM 跟踪。https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

public static final String MODULE_ICON_PACK_FREE = "com.example.iconpack_free";
public static final String APP_STORE_URI =
        "market://details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";
public static final String APP_STORE_GENERIC_URI =
        "https://play.google.com/store/apps/details?id=%s&referrer=utm_source=%s&utm_medium=app&utm_campaign=plugin";

try {
    startActivity(new Intent(
        Intent.ACTION_VIEW,
        Uri.parse(String.format(Locale.US,
            APP_STORE_URI,
            MODULE_ICON_PACK_FREE,
            getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(
        Intent.ACTION_VIEW,
        Uri.parse(String.format(Locale.US,
            APP_STORE_GENERIC_URI,
            MODULE_ICON_PACK_FREE,
            getPackageName()))).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
于 2018-12-14T09:55:59.083 回答
1

具有回退和当前语法的 kotlin 版本

 fun openAppInPlayStore() {
    val uri = Uri.parse("market://details?id=" + context.packageName)
    val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)

    var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
    flags = if (Build.VERSION.SDK_INT >= 21) {
        flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
    } else {
        flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }

    goToMarketIntent.addFlags(flags)

    try {
        startActivity(context, goToMarketIntent, null)
    } catch (e: ActivityNotFoundException) {
        val intent = Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))

        startActivity(context, intent, null)
    }
}
于 2019-02-22T20:36:32.123 回答
0

对于费率应用:重定向到 Playstore。在 Flutter 中,你可以通过 Platform 通道来做到这一点,就像这样

颤振部分:-

 static const platform = const MethodChannel('rateApp');   // initialize 

点按: platform.invokeMethod('urls', {'android_id': 'com.xyz'}),

现在 Android 原生部分(Java):

private static final String RATEAPP = "rateApp";  // initialize variable

// 现在在 ConfigureFlutterEngine 函数中:

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), RATEAPP)
            .setMethodCallHandler(
                    (call, result) -> {               
                        if (call.method.equals("urls") && call.hasArgument("android_id")) {
                            String id = call.argument("android_id").toString();
                          
                            try {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("$uri" + id)));
                            } catch (android.content.ActivityNotFoundException anfe) {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id)));
                            }
                            result.success("Done");
                        } else {
                            result.notImplemented();
                        }
                    }
            );
于 2021-08-03T06:04:31.943 回答