21

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;
}
TacB0sS
  • 10,106
  • 12
  • 75
  • 118

6 Answers6

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;
        }
    }
}
rekire
  • 47,260
  • 30
  • 167
  • 264
ScouseChris
  • 4,377
  • 32
  • 38
  • That means that now I have to check for the existence of both apps... Hmm... not very nice! – TacB0sS May 11 '12 at 12:48
  • 2
    But why do you use flag `PackageManager.GET_UNINSTALLED_PACKAGES` in `getInstalledPackages()` and not just `getInstalledPackages(0)`? – Prizoff Nov 29 '12 at 00:01
  • 6
    By the way, not `"com.google.vending"`, but `"com.android.vending"`. Not sure about old name. – Prizoff Nov 29 '12 at 11:59
  • Thanks, one comment: declaration of packageManager must be: PackageManager packageManager and also want to say the same as Prizoff. – Codebeat Mar 15 '14 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 Aug 12 '14 at 14:25
  • 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 packages – user924 Dec 09 '18 at 18:06
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)

codingjeremy
  • 5,215
  • 1
  • 36
  • 39
saiyancoder
  • 1,285
  • 2
  • 13
  • 20
  • 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 May 06 '13 at 20:08
  • It is indeed! But well, is what Google is used to doing :) – saiyancoder Jul 24 '13 at 02:53
  • 10
    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 Apr 11 '14 at 06:48
  • Absolutely. Would you like to edit my comment so that it includes this? – saiyancoder Apr 11 '14 at 20:11
  • This question is not about services. This answer should not be here at all. – Renetik Dec 10 '22 at 07:15
  • @Renetik 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. – saiyancoder Jan 08 '23 at 18:32
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;
    }
}
MrMaffen
  • 1,647
  • 1
  • 17
  • 22
  • 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 Jun 20 '16 at 10:17
  • 1
    @Thomas, not necessary. you can replace GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE with "com.android.vending" – Andriy Antonov Feb 19 '18 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.
}
AndyW
  • 1,431
  • 16
  • 22
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;
            }
pioneerBhawna
  • 578
  • 7
  • 15
  • 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 Aug 16 '14 at 11:01
  • `isGooglePlayServicesAvailable` is deprecated. Use `GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)` instead. – Timo Bähr Dec 04 '19 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;
   }
vitiello.antonio
  • 323
  • 3
  • 12