简体   繁体   English

无法确定 Android 设备上是否安装了 Google Play 商店

[英]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:这是检查设备上已安装软件包的简单问题……在我将操作系统升级到 2.3.5 之前,我可以使用以下代码找到 Market/Play 商店:

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.由于某些原因更新后,我根本找不到指示应用程序已安装的to包名称,尽管它在设备上,并且我可以访问市场。

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.请注意,这个将近 5 年的旧代码不是最佳的,Google 不喜欢您在没有充分理由的情况下检查所有已安装的软件包。 Please check also the other answers.另请检查其他答案。

The package name has changed, it is now com.android.vending包名变了,现在是 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: GooglePlayServices 有一个实用程序类,其中包含处理此问题的方法:

isGooglePlayServicesAvailable(Context) . isGooglePlayServicesAvailable(Context)

It provides appropriate error dialogs for the status of play services on the device.它为设备上的播放服务状态提供适当的错误对话框。

API Reference: API参考:

GoogleApiAvailability.isGooglePlayServicesAvailable(android.content.Context) GoogleApiAvailability.isGooglePlayServicesAvailable(android.content.Context)

As Michael stated in the comments Google Play Services is not the same as the Google Play Store.正如迈克尔在评论中所说的那样,Google Play 服务与 Google Play 商店不同。 Use this to determine whether or not the Play Store is installed on your device:使用它来确定您的设备上是否安装了 Play 商店:

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.在大多数情况下,我们想知道是否安装了 Google Play 商店以在预加载一些应用程序页面的情况下启动它。

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.使用此代码,您可以检查您的设备上是否安装了Google Play 服务

int val=GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
            if(val==ConnectionResult.SUCCESS)
            {
                play_installed=true;
            }
            else
            {
                play_installed=false;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PiracyChecker 无法检查应用程序是否从 Google Play 商店安装 - PiracyChecker fails to check whether the app is installed from Google Play Store Android:该设备与Google Play商店不兼容的解决方案 - Android: Solution for the device not compatible with the Google Play Store 如何检测Android设备上是否已安装Google Apps - How to detect whether google apps are installed or not on a android device Android:如何确定Google Play服务是否已正确安装 - Android: How to determine if Google Play Services was correctly installed 如果从 Google Play 商店下载 apk 和 obb,则无法在 android 10 设备上访问 obb 扩展文件 - Cannot reach obb expansion file on android 10 device if apk and obb is downloaded from Google Play store 如何检查Android设备是否已安装Google Play? - How to check if an android device has Google Play installed? 如何以编程方式确定是否可以在当前设备上安装Play商店中的应用程序? - How do I programmatically determine if an app in the play store can be installed on current device? 如何从Android中的google play商店获取已安装应用程序的列表? - How to get the list of installed application from google play store in android? 为什么在适用于Android的Google Play商店中将特定设备标记为“不受支持” - Why is a specific device marked as 'unsupported' in Google Play Store for Android 通过Android设备目录加入Google PLAY存储数据 - JOIN Google PLAY store data with Android Device Catalog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM