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

后端 未结 23 2749
既然无缘
既然无缘 2020-11-22 02:00

I have open the Google Play store using the following code

Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse(\"https://play.goo         


        
相关标签:
23条回答
  • 2020-11-22 02:05

    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 official link

    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>

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

    Linking to a Product List

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

    From an Android app: 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

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

    0 讨论(0)
  • 2020-11-22 02:05

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

    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 Exception will be caught.

    0 讨论(0)
  • 2020-11-22 02:07

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

    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...

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

    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.

    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_TOP could fix the problem. (I didn't test it myself yet...)

    See this answer for understanding what Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED does.

    0 讨论(0)
  • 2020-11-22 02:07

    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

    0 讨论(0)
  • 2020-11-22 02:07

    Kotlin:

    Extension:

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

    Method:

        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")
                    )
                )
            }
        }
    
    0 讨论(0)
  • 2020-11-22 02:07

    If you want to open Google Play store from your app then use this command directy: market://details?gotohome=com.yourAppName, it will open your app's Google Play store pages.

    • Web: http://play.google.com/store/apps/details?id=
    • App: market://details?id=

    Show all apps by a specific publisher

    • Web: http://play.google.com/store/search?q=pub:
    • App: market://search?q=pub:

    Search for apps that using the Query on its title or description

    • Web: http://play.google.com/store/search?q=
    • App: market://search?q=

    Reference: https://tricklio.com/market-details-gotohome-1/

    0 讨论(0)
提交回复
热议问题