Home » Android » Redirect User to Play store From Android App

Redirect User to Play store From Android App

Posted by: admin May 14, 2020 Leave a comment

Questions:

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();
            }
        }
    });
How to&Answers:

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

Answer:

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

Answer:

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