Neil Castellino
Neil Castellino

Reputation: 79

How to avoid opening other android stores and open only Google Play Store on Android programmatically?

In many Mi phones, when I use this code to open the Google play store, the MI store opens.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));

How do I avoid that and check if the Google play store is installed and open only the Google play store?

Upvotes: 4

Views: 2378

Answers (1)

Ryan M
Ryan M

Reputation: 20119

You can set the Intent's package to com.android.vending, which is the package name of the Play Store app. This will make it so that only that app will receive the Intent.

Based on Linking to Google Play:

Kotlin:

val appPackageName = "your.package.name.here"
val intent = Intent(Intent.ACTION_VIEW).apply {
    data = Uri.parse(
            "https://play.google.com/store/apps/details?id=$appPackageName")
    setPackage("com.android.vending")
}
startActivity(intent)

Java:

String appPackageName = "your.package.name.here";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
        "https://play.google.com/store/apps/details?id=" + appPackageName));
intent.setPackage("com.android.vending");
startActivity(intent);

If the Play Store is not installed, the startActivity call will throw an ActivityNotFoundException, which you can catch.

I believe this would also work with the market:// URLs, but I haven't tested that since I don't have a Mi phone to confirm.

Upvotes: 4

Related Questions