16

Hello Friends i am developing an app , i had an requirement to redirect user to play store from my app , i searched a lot but no success .. code is below

Button1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v_arg) {
            try {
                        Intent viewIntent =
                        new Intent("android.intent.action.VIEW",
                        Uri.parse("https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla/"));
                        startActivity(viewIntent);
            }catch(Exception e) {
                Toast.makeText(getApplicationContext(),"Unable to Connect Try Again...",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });

    Button2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v_arg) {
            try {
                        Intent viewIntent =
                        new Intent("android.intent.action.VIEW",
                        Uri.parse("market://details?id=com.adeebhat.rabbitsvilla/"));
                        startActivity(viewIntent);
            }catch(Exception e) {
                Toast.makeText(getApplicationContext(),"Unable to Connect Try Again...",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
ADIL BHAT
  • 193
  • 1
  • 1
  • 12
  • 1
    Your code seems to be ok. Did you got any error ? – Spring Breaker Apr 24 '14 at 08:57
  • possible duplicate of [How to open the Google Play Store directly from my Android application?](http://stackoverflow.com/questions/11753000/how-to-open-the-google-play-store-directly-from-my-android-application) – TripeHound Apr 24 '14 at 09:01

3 Answers3

36

More sophisticated way:

final String appPackageName = getPackageName(); // package name of the app
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)));
}
Tushar Monirul
  • 4,944
  • 9
  • 39
  • 49
  • 1
    Just be careful when you are using `applicationIdSuffix` in different `buildTypes` or this will fail when you build in a non-production build because the packageName will be different than what you have on Google Play store. One package might be something like `com.youpackage.debug` and the release one something like `com.youpackage`. – Leonardo Sibela Aug 06 '20 at 12:46
14

You just need to remove character "/" from the url

So will be

https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla/

to

https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla

So finally

Button1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v_arg) {
        try {
                    Intent viewIntent =
                    new Intent("android.intent.action.VIEW",
                    Uri.parse("https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla"));
                    startActivity(viewIntent);
        }catch(Exception e) {
            Toast.makeText(getApplicationContext(),"Unable to Connect Try Again...",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
});
Piyush
  • 18,895
  • 5
  • 32
  • 63
11

Remove slash from url. You have added extra slash after package name.

https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla/

It should be

https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla

Both uri should be

Uri.parse("https://play.google.com/store/apps/details?id=com.adeebhat.rabbitsvilla")); // Removed slash

and

Uri.parse("market://details?id=com.adeebhat.rabbitsvilla")); // Removed slash
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186