3

I have written a code to list all the installed apps. I have a button event which will be redirected to the particular App home page in the Google Store. However I am unable to successfully redirect them. Here is the code I have employed. I am not getting the app's package name in the uri and hence I am getting "URL not found on the server". Kindly drop in your valuable suggestions.

public void update(View view) {
    Context context = this;
    Uri uri = Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName());
    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
    // To count with Play market backstack, After pressing back button,
    // to taken back to our application, we need to add following flags to intent.
    try {
        startActivity(goToMarket);
    } catch (ActivityNotFoundException e) {
        startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
    }
}

Thanks in advance

user4232
  • 592
  • 1
  • 12
  • 37
  • you are using the pkg name of your own app `context = this; context.getPackageName()` you need to use the package name of the other app – SMR Oct 14 '15 at 07:09
  • Hey thanks for the suggestion. How do I dynamically set the context to redirect to the package name of the other app –  Oct 14 '15 at 07:13
  • I think [this](http://stackoverflow.com/a/5097838/3193867) is what you need. not context. – SMR Oct 14 '15 at 07:23

1 Answers1

2

To Open a Native Play store page you must use this play store schema

market://details?id=your.app.package.name

But keep in mind that may be the device doesn't have Google play services or the play store itself.

So to avoid crashing, catch the exception happened and try to open the normal URL

public static void openPlayStorePage(Activity parentActivity, String targetPackageName) {

        try {
            parentActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + targetPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            parentActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + targetPackageName)));
        }
    } 
Simon K. Gerges
  • 3,097
  • 36
  • 34