How to open the Google Play Store from Android application Android 10.10.2017

How to open the Google Play Store from Android application

Google Play provides several link formats that let you bring users to your products in the way you want, from Android apps, web pages, ads, reviews, articles, social media posts, and more.

There are two general formats for links that are accessible to users on Android devices. The two formats trigger slightly different behaviors on the device:

  • market://. Launches the Play Store app to load the target page.
  • http://. Lets the user choose whether to launch the Play Store app or the browser to handle the request. If the browser handles the request, it loads the target page on the Google Play website.

If you want to link to your products from an Android app, create an Intent that opens a Google Play URL, as shown in the example below.

final String appPackageName = getPackageName();

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

We use a try/catch block here because an Exception will be thrown if the Play Store is not installed on the target device.

Note: Any app can register as capable of handling the market://details?id=<appId> uri, if you want to specifically target Google Play check this link.

In Kotlin we can use following snippet

val uri = Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID)
var intent = Intent(Intent.ACTION_VIEW, uri)

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY or
        Intent.FLAG_ACTIVITY_NEW_DOCUMENT or
        Intent.FLAG_ACTIVITY_MULTIPLE_TASK)

if (intent.resolveActivity(activity.packageManager) != null) {
    startActivity(intent)
} else {
    intent = Intent(Intent.ACTION_VIEW,
        Uri.parse("http://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID))
    if (intent.resolveActivity(activity.packageManager) != null) {
        startActivity(intent)
    } else {
        Toast.makeText(activity, "No play store or browser app", Toast.LENGTH_LONG).show()
    }
}