Cannot determine whether Google play store is installed or not on Android device
Asked Answered
S

6

22

This was a simple matter of checking the installed packages on the device... before I've upgraded my OS to 2.3.5, I could locate the Market/Play store, using this code:

private static final String GooglePlayStorePackageName = "com.google.market";

void someMethod() {
    packageManager = getApplication().getPackageManager();
    List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (PackageInfo packageInfo : packages) {
        if (packageInfo.packageName.equals(GooglePlayStorePackageName)) {
            googlePlayStoreInstalled = true;
            break;
        }
    }
}

For some reason after the update, I simply cannot find the to package name to indicate the application is installed, although it is on the device, and I can access the market.

Has the package name changed? or perhaps I'm looking at this the wrong way?

Thanks,

Adam.

UPDATE:

That was a stupid way to check if a package is installed... a better way is:

protected final boolean isPackageInstalled(String packageName) {
    try {
        application.getPackageManager().getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}
Schizopod answered 11/5, 2012 at 12:41 Comment(1)
Above code is working perfectly fine...thanxBiblicist
H
31

Be aware that this almost 5 years old code is not optimal and Google does not like when you check all installed packages without no good reason. Please check also the other answers.

The package name has changed, it is now com.android.vending


Try:

private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";

void someMethod() {
    PackageManager packageManager = getApplication().getPackageManager();
    List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (PackageInfo packageInfo : packages) {
        if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld) ||
            packageInfo.packageName.equals(GooglePlayStorePackageNameNew)) {
            googlePlayStoreInstalled = true;
            break;
        }
    }
}
Haunch answered 11/5, 2012 at 12:47 Comment(6)
That means that now I have to check for the existence of both apps... Hmm... not very nice!Schizopod
But why do you use flag PackageManager.GET_UNINSTALLED_PACKAGES in getInstalledPackages() and not just getInstalledPackages(0)?Starwort
By the way, not "com.google.vending", but "com.android.vending". Not sure about old name.Starwort
Thanks, one comment: declaration of packageManager must be: PackageManager packageManager and also want to say the same as Prizoff.Loricate
@Prizoff: you can disable the Google Play Store App. So while it is not counted in installed apps, it will be picked up when we pass the flag GET_UNINSTALLED_PACKAGES.Oecd
Then why not using this method context.getPackageManager() .getLaunchIntentForPackage("com.google.market") != null || context.getPackageManager() .getLaunchIntentForPackage("com.android.vending") != null)? It doesn't require you to loop through all packagesBragi
B
12

GooglePlayServices has a utility class with a method to handle this:

isGooglePlayServicesAvailable(Context).

It provides appropriate error dialogs for the status of play services on the device.

API Reference:

GoogleApiAvailability.isGooglePlayServicesAvailable(android.content.Context)

Byrom answered 6/5, 2013 at 19:56 Comment(6)
This is very recent... I'm about to test it in the next few days... so far the code looks like one big mess!Schizopod
It is indeed! But well, is what Google is used to doing :)Byrom
Notice that developers should not confuse the Google Play store app and Google Play Service. GooglePlayServicesUtil.isGooglePlayServicesAvailable() checks for the latter one which might be an indicator but not a requirement. The class has constants for both package names, though. IMHO, if you want to check for an existing Google Play store app (e.g. because you want to launch a market:// intent, use the PackageManager with GOOGLE_PLAY_STORE_PACKAGE.Capsaicin
Absolutely. Would you like to edit my comment so that it includes this?Byrom
This question is not about services. This answer should not be here at all.Unity
@Unity the question (and answer) are about GooglePlayServices and how to detect if GooglePlay is installed. It is not about services in general, so it is fine to be here. Also, question tags are "android" and "google-play", so not sure what you mean.Byrom
A
9

As Michael stated in the comments Google Play Services is not the same as the Google Play Store. Use this to determine whether or not the Play Store is installed on your device:

public static boolean isPlayStoreInstalled(Context context){
    try {
        context.getPackageManager()
                .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
Ahmadahmar answered 10/2, 2016 at 15:39 Comment(2)
Note that this requires that you add com.google.android.gms:play-services-base:$playServicesVersion to your dependencies. (It doesn't create a run-time dependency on Play Services being installed.)Stahl
@Thomas, not necessary. you can replace GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE with "com.android.vending"Muna
C
2

In most case we want to find out whether Google Play Store is installed or not to launch it with some app page preloaded.

Why cant we do this:

final String appPackageName = getPackageName(); // get your app package name
try {
    Uri uri = Uri.parse("market://details?id=" + appPackageName);
    startActivity(new Intent(Intent.ACTION_VIEW, uri));
} catch (android.content.ActivityNotFoundException anfe) {
    // Google Play Store is not available.
}
Concupiscence answered 29/3, 2017 at 22:21 Comment(0)
B
0

Using this code, you can check Google Play Services are installed on your device or not.

int val=GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
            if(val==ConnectionResult.SUCCESS)
            {
                play_installed=true;
            }
            else
            {
                play_installed=false;
            }
Biblicist answered 17/7, 2013 at 7:17 Comment(2)
Google Play Service != Google Play Store. You can have the store installed but not GPS and vice versa. The OP was asking for the store.Capsaicin
isGooglePlayServicesAvailable is deprecated. Use GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) instead.Abruption
R
0

To check if Google Play Store is installed and activated:

Kotlin

companion object {
    private const val GOOGLE_PLAY_STORE_PACKAGE = "com.android.vending"
}

private fun isPlayStoreInstalled(context: Context): Boolean {
    return try {
        val packageInfo = context.packageManager.getPackageInfo(GOOGLE_PLAY_STORE_PACKAGE, 0)
        packageInfo.applicationInfo.enabled
    } catch (exc: PackageManager.NameNotFoundException) {
        false
    }
}

Java

   private static final String GOOGLE_PLAY_STORE_PACKAGE = "com.android.vending";

   private final boolean isPlayStoreInstalled(Context context) {
      boolean flag;
      try {
         PackageInfo packageInfo = context.getPackageManager().getPackageInfo(GOOGLE_PLAY_STORE_PACKAGE, 0);
         flag = packageInfo.applicationInfo.enabled;
      } catch (PackageManager.NameNotFoundException exc) {
         flag = false;
      }
      return flag;
   }
Reticule answered 2/9, 2021 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.