Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 625
Rajesh Kumar Avatar asked Aug 01 '12 05:08

Rajesh Kumar


People also ask

How do I force the Play Store to open?

If the Play Store suddenly goes frozen, you can try force stopping the app. Step 1: Go to Settings -> Apps -> All. Step 2: Then you can select the Play Store and tap on the Force Stop button. If there's still an issue after this, then perhaps you have not downloaded the latest version of the Google Play Store.


2 Answers

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

like image 62
Cat Avatar answered Oct 05 '22 23:10

Cat


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.

like image 25
Berťák Avatar answered Oct 06 '22 00:10

Berťák