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

时间:2012-08-01 05:27:10

标签: 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);.

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

24 个答案:

答案 0 :(得分:1308)

您可以使用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/catch阻止,因为如果目标设备上未安装Play商店,则会引发Exception

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

答案 1 :(得分:147)

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

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

要打开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);
    }
}

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

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

请参阅this answer了解Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED的作用。

答案 2 :(得分:69)

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

Android开发者官方链接

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

关联到应用程序页面

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

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

关联商品列表

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

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

关联搜索结果

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

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

答案 3 :(得分:23)

试试这个

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

答案 4 :(得分:18)

如果您确实想要独立打开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);

重要的是,实际上是独立开启Google Play或任何其他应用。

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

的问候。

答案 5 :(得分:12)

您可以检查是否安装了 Google Play商店应用,如果是这种情况,您可以使用&#34; market://&#34; protocol。

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

答案 6 :(得分:10)

使用market://

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

答案 7 :(得分:9)

虽然Eric的回答是正确的,但Berťák的代码也有效。我认为这更优雅地结合了两者。

setPackage

使用Exception,您强制设备使用Play商店。如果没有安装Play商店,则会捕获{{1}}。

答案 8 :(得分:6)

你可以这样做:

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

获取参考here

您还可以尝试此问题的接受答案中描述的方法: Cannot determine whether Google play store is installed or not on Android device

答案 9 :(得分:4)

Ready-to-use solution:

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

}

Based on Eric's answer.

答案 10 :(得分:4)

在聚会Official docs中很晚在这里。所描述的代码是

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商店应用中而不是选择器中查看您应用的详细信息。 用于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)

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

对于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)

答案 11 :(得分:2)

此问题的某些答案已过时。

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

”“如果要通过Android应用链接到您的产品,请创建一个 打开URL的意图。在配置此意图时,请通过 将“ 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)

我希望能对某人有所帮助!

答案 12 :(得分:1)

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

答案 13 :(得分:1)

科特琳:

扩展名:

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

答案 14 :(得分:1)

由于the official docs使用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应用打开,并恢复为默认状态。

答案 15 :(得分:1)

以上是上述答案中的最终代码,首次尝试使用Google 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);
        }
    }

答案 16 :(得分:1)

我的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))

答案 17 :(得分:1)

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

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

搜索在标题或说明中使用查询的应用

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

答案 18 :(得分:0)

如果您使用的是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

答案 19 :(得分:0)

科特林

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

答案 20 :(得分:0)

人民,别忘了您实际上可以从中得到更多。我的意思是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));
}

答案 21 :(得分:0)

具有回退和当前语法的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)
    }
}

答案 22 :(得分:0)

我结合了Berťák Stefano 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, "");
}

答案 23 :(得分:0)

对于费率应用程序:重定向到 Playstore。 在 Flutter 中,你可以通过一个 Platform channel 来做到这一点

颤振部分:-

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

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

现在安卓原生部分(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();
                        }
                    }
            );