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

845,926

Solution 1

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

Java

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

Kotlin

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

We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.

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ťák answer

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

try this

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
Share:
845,926
Rajesh Kumar
Author by

Rajesh Kumar

Updated on August 03, 2021

Comments

  • Rajesh Kumar
    Rajesh Kumar almost 3 years

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

  • Rajesh Kumar
    Rajesh Kumar almost 12 years
    I have tried already with this code, this is also shows the option to select the browser/play store, because my device have installed both apps(google play store/browser).
  • Rajesh Kumar
    Rajesh Kumar almost 12 years
    I have tried already with this code, this is also shows the option to select the browser/play store, because my device have installed both apps(google play store/browser).
  • Cat
    Cat almost 12 years
    Not sure what to tell you. I use this code on both 2.3.3 and 4.1 and both open up the Play Store. Additionally, the docs say market:// "Launches the Play Store app".
  • Stefano Munarini
    Stefano Munarini almost 11 years
    if you want to redirect to all Developer's apps use market://search?q=pub:"+devName and http://play.google.com/store/search?q=pub:"+devName
  • Nathiya
    Nathiya almost 11 years
    thank u for the post. Me also tried with this code but it shows the option to select the browser/playstore. rajesh if u got the answer for this issue means update the post.
  • code4jhon
    code4jhon almost 10 years
    For how to open google play independently (not embedded in a new view in the same app) please check my answer.
  • code4jhon
    code4jhon almost 10 years
    For how to open google play independently (not embedded in a new view in the same app) please check my answer.
  • code4jhon
    code4jhon almost 10 years
    For how to open google play independently (not embedded in a new view in the same app) please check my answer.
  • code4jhon
    code4jhon almost 10 years
    For how to open google play independently (not embedded in a new view in the same app) please check my answer.
  • Cat
    Cat almost 10 years
    What is this.cordova? Where are the variable declarations? Where is callback declared and defined?
  • code4jhon
    code4jhon almost 10 years
    this is part of a Cordova plugin, I don't think that is actually relevant ... you just need an instance of PackageManager and start an activity in a regular way but this is the cordova plugin of github.com/lampaa which I overwrote here github.com/code4jhon/org.apache.cordova.startapp
  • Cat
    Cat almost 10 years
    My point is simply that, this code isn't really something that people can simply port to their own app for use. Trimming the fat and leaving just the core method would be useful to future readers.
  • code4jhon
    code4jhon almost 10 years
    Yes, I understand ... for now I am on hybrid apps. Can't really test completely native code. But I think the idea is there. If I have a chance I will add exact native lines.
  • code4jhon
    code4jhon almost 10 years
    hopefully this will make it @eric
  • Sagi Mann
    Sagi Mann over 9 years
    What if I want to open Play with a 'referrer' param? Am I expected to simply add it to the URI, e.g. "market://xxxxx&referrer=abc" ? Or do I need to update some other part of the Intent/action/etc?
  • Cat
    Cat over 9 years
    @SagiMann-TROPHiT Honestly, I'm not sure. You'd be better off starting a new question.
  • ooolala
    ooolala over 9 years
    Doesn't the web link need https now?
  • Cat
    Cat over 9 years
    @ooolala Need, no. Should have, yes. Will change that.
  • offset
    offset over 9 years
    Can I return to my app after the download is finish? Thanks.
  • Cat
    Cat over 9 years
    @offset No, unfortunately not. There might be an Intent you could capture on package addition, but even then I don't think the OS provides you the control to switch over to your app. (Just to do something in the background.)
  • Berťák
    Berťák over 9 years
    This solution does not work, if some application uses intent filter with "market://" scheme defined. See my answer how to open Google Play AND ONLY Google Play application (or webbrowser if GP not present). :-)
  • Christian García
    Christian García almost 9 years
    For projects using the Gradle build system, appPackageName is in fact BuildConfig.APPLICATION_ID. No Context/Activity dependencies, reducing the risk of memory leaks.
  • wblaschko
    wblaschko over 8 years
    You still need the context to launch the intent. Context.startActivity()
  • zoltish
    zoltish over 8 years
    While this is good it also seems unreliable with the current Google Play build, if you enter another apps page on Google Play then trigger this code, it will just open Google Play but not go to your app.
  • TrevorWiley
    TrevorWiley about 8 years
    @zoltish, I've added Intent.FLAG_ACTIVITY_CLEAR_TOP to the flags and that seems to fix the problem
  • Violet Giraffe
    Violet Giraffe almost 8 years
    Does it work for you? It opens the main Google Play page, not my app's page.
  • Coda
    Coda almost 8 years
    This solution assumes there is an intent to open a web browser. This isn't always true (like on Android TV) so be cautious. You may want to use intent.resolveActivity(getPackageManager()) to determine what to do.
  • Praveen Kumar Verma
    Praveen Kumar Verma almost 8 years
    I have used Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED but not work. no any new Instance open of Play store
  • dum4ll3
    dum4ll3 almost 8 years
    What happens if you use rateIntent.setPackage("com.android.vending") to be sure the PlayStore app gonna handle this intent, Instead of all this code?
  • Daniele Segato
    Daniele Segato over 7 years
    @dum4ll3 I guess you can but this code also implicitly check if the Google Play app is installed. If you do not check it you need to catch for ActivityNotFound
  • MPaulo
    MPaulo over 7 years
    Developer is now: From a website: play.google.com/store/dev?id=<developer_id> From an Android app: market://dev?id=<developer_id>
  • Aetherna
    Aetherna over 6 years
    Id suggest divide this code into smaller methods. It's hard to find important code in this spaghetti :) Plus you're checking for "com.android.vending" what about com.google.market
  • Dhaval Rajani
    Dhaval Rajani over 6 years
    market:// is not working if i am redirecting from another domain url. For example if user is opening url example.com/blabla in his browser and i am simply redirecting to market://details?id=com.example.blabla previously its working but from 1 week onwards its not working. can anyone help me regarding this. i am using nodejs for URL redirection.
  • Bostrot
    Bostrot about 6 years
    @DhavalRajani It wouldn't work on my emulator but on real hardware for me.
  • Ashwin
    Ashwin over 5 years
    I think you should use application id instead of package name.
  • CoolMind
    CoolMind over 5 years
    I think, this is wrong, at least, Uri.parse("https://play.google.com/store/apps/details?id=. On some devices it opens web-browser instead of Play Market.
  • Husnain Qasim
    Husnain Qasim over 5 years
    All the code is taken from official docs. Link is also attached in answer code is described here for a quick reference.
  • Greg Ennis
    Greg Ennis over 5 years
    Using market:// prefix is not recommended anymore (check the link you posted)
  • hpAndro
    hpAndro over 5 years
    Actually in my task, there is an webview and in webview I have to load any URL. but in that if there is open any playstore url it shows open playstore button. so I need to open app on click of that button. it will be dynamic for any application, so how can I manage?
  • Nikolay Shindarov
    Nikolay Shindarov over 5 years
    Just try the link https://play.app.goo.gl/?link=https://play.google.com/store/‌​apps/details?id=com.‌​app.id&ddl=1&pcampai‌​gnid=web_ddl_1
  • serv-inc
    serv-inc about 5 years
    The official docs use https://play.google.com/store/apps/details?id= instead of market: How come? developer.android.com/distribute/marketing-tools/… Still a comprehensive and short answer.
  • Dr.jacky
    Dr.jacky over 4 years
    You can use getLaunchIntentForPackage("com.android.vending") instead of the first for if.
  • zeus
    zeus over 4 years
    @GregEnnis where you see that market:// prefix is not recommended anymore ?
  • tir38
    tir38 over 4 years
    @loki I think the point is that it's not listed as a suggestion any more. If you search that page for the word market you won't find any solution. I think the new way is to fire off a more generic intent developer.android.com/distribute/marketing-tools/… . More recent versions of the Play Store app probably have an intent filter for this URI https://play.google.com/store/apps/details?id=com.example.an‌​droid
  • tir38
    tir38 over 4 years
    @CoolMind the reason for that is probably because those devices have an older version of Play Store app which don't have an intent filter matching that URI.
  • CoolMind
    CoolMind over 4 years
    @tir38, maybe so. Maybe they don't have Google Play Services or not authorized in them, I don't remember.
  • kuchi
    kuchi about 3 years
    I tried Berťák's code and it works but this one is much simpler.
  • Maksymilian Tomczyk
    Maksymilian Tomczyk about 3 years
    I'm using this solution in Kotlin. Opening the app page in PlayStore works, but starting webIntent crashes the app. Not sure if it's correct solution, but it can be fixed by adding webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) before context.startActivity(webIntent)