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;
}
1
  • Above code is working perfectly fine...thanx Commented Jul 1, 2013 at 12:17

6 Answers 6

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;
        }
    }
}
6
  • That means that now I have to check for the existence of both apps... Hmm... not very nice!
    – TacB0sS
    Commented May 11, 2012 at 12:48
  • 2
    But why do you use flag PackageManager.GET_UNINSTALLED_PACKAGES in getInstalledPackages() and not just getInstalledPackages(0)?
    – Prizoff
    Commented Nov 29, 2012 at 0:01
  • 6
    By the way, not "com.google.vending", but "com.android.vending". Not sure about old name.
    – Prizoff
    Commented Nov 29, 2012 at 11:59
  • Thanks, one comment: declaration of packageManager must be: PackageManager packageManager and also want to say the same as Prizoff.
    – Codebeat
    Commented Mar 15, 2014 at 20:29
  • @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.
    – rajankz
    Commented Aug 12, 2014 at 14:25
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)

6
  • 1
    This is very recent... I'm about to test it in the next few days... so far the code looks like one big mess!
    – TacB0sS
    Commented May 6, 2013 at 20:08
  • It is indeed! But well, is what Google is used to doing :) Commented Jul 24, 2013 at 2:53
  • 11
    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.
    – Michael
    Commented Apr 11, 2014 at 6:48
  • Absolutely. Would you like to edit my comment so that it includes this? Commented Apr 11, 2014 at 20:11
  • This question is not about services. This answer should not be here at all.
    – Renetik
    Commented Dec 10, 2022 at 7:15
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;
    }
}
2
  • 1
    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.)
    – Thomas
    Commented Jun 20, 2016 at 10:17
  • 1
    @Thomas, not necessary. you can replace GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE with "com.android.vending" Commented Feb 19, 2018 at 10:16
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.
}
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;
            }
2
  • 5
    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.
    – Michael
    Commented Aug 16, 2014 at 11:01
  • isGooglePlayServicesAvailable is deprecated. Use GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) instead.
    – Timo Bähr
    Commented Dec 4, 2019 at 11:05
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;
   }

Not the answer you're looking for? Browse other questions tagged or ask your own question.