89

I have link of my other apps in my latest app, and I open them in that way.

Uri uri = Uri.parse("url");
Intent intent = new Intent (Intent.ACTION_VIEW, uri); 
startActivity(intent);

this code opens the browser version of google play store.

When trying to open from my phone, the phone prompts if I want to use a browser or google play and if I choose the second one it opens the mobile version of google play store.

Can you tell me how can this happen at once? I mean not ask me but directly open the mobile version of google play, the one that I see while open it directly from phone.

starball
  • 20,030
  • 7
  • 43
  • 238
ghostrider
  • 5,131
  • 14
  • 72
  • 120
  • 1
    I wish that the second to last paragraph was true for me. Using the http link found here: http://developer.android.com/distribute/googleplay/promote/linking.html#OpeningDetails does not prompt for the user to choose the app or the browser. It always assumes the browser. Unfortunately, I can't use the `market://` protocol either. Anybody else seeing this behavior? – SilithCrowe Jun 28 '13 at 16:24

6 Answers6

264

You'll want to use the specified market protocol:

final String appPackageName = "com.example"; // Can also use getPackageName(), as below
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));

Keep in mind, this will crash on any device that does not have the Market installed (the emulator, for example). Hence, I would suggest something like:

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("http://play.google.com/store/apps/details?id=" + appPackageName)));
}

While using getPackageName() from Context or subclass thereof for consistency (thanks @cprcrack!). You can find more on Market Intents here: link.

Community
  • 1
  • 1
Cat
  • 66,919
  • 24
  • 133
  • 141
  • Opening the following in the browser does not open Google Play: market://details?id=pandora – IgorGanapolsky Jan 13 '13 at 19:04
  • 1
    The ID is the package name, not the app name. Try `market://details?id=com.PandoraTV` (assuming [this](https://play.google.com/store/apps/details?id=com.PandoraTV) is the app you want). – Cat Jan 13 '13 at 19:32
  • Try opening any link that starts with market:// in Chrome for Android. You will see that it only performs a search result, it doesn't open the Google Play store. It is irrelevant if the Pandora package name is correct here, I can give you a hundred correct package names and it won't change the issue. – IgorGanapolsky Jan 14 '13 at 20:26
  • 9
    **This answer is about using the `market://` prefix from your own app, not from a website via the browser.** I can attest to its functionality (on versions 2.3, 3.x, 4.0, 4.1, and 4.2) and it works with the stock browser, Chrome Beta 25, and Chrome 18. – Cat Jan 14 '13 at 20:30
  • I got it, thanks. I would also think that Chrome is considered an app, and it treats market:// specially just like my app would. – IgorGanapolsky Jan 15 '13 at 17:11
  • 1
    Yes, Chrome is an app, but that would require its makers [Google] to use this code, not the people [you] making the websites. As it stands, this code is to help people who are structuring links specifically from their apps. – Cat Jan 15 '13 at 17:55
  • 2
    You can use `getPackageName()` to automatically retrieve the app id. – cprcrack May 01 '13 at 23:37
  • How to use this link in share message? – Pratik Butani Dec 03 '14 at 18:09
  • @ツPratikButaniツ I don't think you can. You could try pasting it as a URL with the `market://` prefix, but many parsers would not recognize it. Best to go with the `play.google.com` URL. – Cat Dec 03 '14 at 18:59
  • But `play.google.com` Opens in browser and may user dont like to go in browser and open. so any way? – Pratik Butani Dec 04 '14 at 04:06
  • @ツPratikButaniツ Shares are not always designed to be opened on a mobile device. The link to the Play Store App has to be done in the code of the app where the link is used. – Cat Dec 04 '14 at 04:31
  • can we get the response from play store app after redirection ? That the user has clicked installed or app gets downloaded through my redirection ? – Gautam Jan 14 '16 at 05:55
  • @Gautam No, I'm afraid not. – Cat Jan 14 '16 at 05:56
  • but i got to know that the advertisement sdk's are tracking this too. – Gautam Jan 14 '16 at 05:59
6

Below code may helps you for display application link of google play sore in mobile version.

For Application link :

Uri uri = Uri.parse("market://details?id=" + mContext.getPackageName());
Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);

  try {
        startActivity(myAppLinkToMarket);

      } catch (ActivityNotFoundException e) {

        //the device hasn't installed Google Play
        Toast.makeText(Setting.this, "You don't have Google Play installed", Toast.LENGTH_LONG).show();
              }

For Developer link :

Uri uri = Uri.parse("market://search?q=pub:" + YourDeveloperName);
Intent myAppLinkToMarket = new Intent(Intent.ACTION_VIEW, uri);

            try {

                startActivity(myAppLinkToMarket);

            } catch (ActivityNotFoundException e) {

                //the device hasn't installed Google Play
                Toast.makeText(Settings.this, "You don't have Google Play installed", Toast.LENGTH_LONG).show();

            } 
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
3

You can use Android Intents library for opening your application page at Google Play like that:

Intent intent = IntentUtils.openPlayStore(getApplicationContext());
startActivity(intent);
Dmitriy Tarasov
  • 1,949
  • 20
  • 37
  • 2
    Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Aug 20 '13 at 09:45
  • 4
    It would be helpful to include a disclaimer that this is your library and [the function is implemented](http://grepcode.com/file/repo1.maven.org/maven2/com.dmitriy-tarasov/android-intents/1.1.0/com/dmitriy/tarasov/android/intents/IntentUtils.java#IntentUtils.openPlayStore%28com.dmitriy.tarasov.android.intents.Context%29) pretty much the same way as the accepted answer. – ta.speot.is Jun 09 '14 at 02:58
1

You can check if the Google Play Store app is installed and, if this is the case, you can use the "market://" 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);
Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37
0

Open app page on Google Play:

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

In case, we want purpose to update by linking to the native google play store application :

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)

and if we want to open the instant app link here is the purpose kotlin code:

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)

Please refer to Android Developer Link : https://developer.android.com/distribute/marketing-tools/linking-to-google-play#android-app

Sofien Rahmouni
  • 4,354
  • 1
  • 21
  • 22