简体   繁体   中英

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

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

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

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)

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

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.
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM