Friday, June 7, 2013

Open Android market (Google Play Store) using Intent

To open google play store (Android market) using intent, just use the code snippet
 

String appName = "";  
Uri uri = Uri.parse("market://details?id=" + appName);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (isAvaialable(intent, mContext)) {
startActivity(intent);  
} else {
Uri uri = Uri.parse("http://play.google.com/store/apps/details?id="+appName);
intent.setData(uri);
startActivity(intent); 
}  

public static boolean isAvailable(Context ctx, Intent intent) {
   final PackageManager mgr = ctx.getPackageManager();
   List list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
   return list.size() > 0;
}
isAvailable() is an utility method that I got from http://www.grokkingandroid.com/checking-intent-availability/

2 comments:

  1. Instead of using one intent and reacting to the exception I think it's better to first check for availability and use the correct one right away.

    I've written a blog post on how to check for Intent availability: http://www.grokkingandroid.com/checking-intent-availability/

    ReplyDelete
    Replies
    1. Creating exception and reacting to it is not a good thing. So I updated my post. Thanks Wolfram.

      Delete

Androidians