问题描述

这是检查设备上已安装软件包的简单问题...在我将操作系统升级到 2.3.5 之前,我可以使用以下代码找到 Market/Play 商店:

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

更新后由于某种原因,我根本找不到表明应用程序已安装的to包名称,虽然它在设备上,我可以访问市场.

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?

谢谢,

亚当.

更新:

这是一种检查是否安装了软件包的愚蠢方法……更好的方法是:

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

推荐答案

请注意,这近 5 年的旧代码并不是最优的,Google 不喜欢无缘无故检查所有已安装的软件包.请同时查看其他答案.

包名已更改,现在是 com.android.vending

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

试试:

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

这篇关于无法确定 Android 设备上是否安装了 Google Play 商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!