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

Cannot determine whether Google play store is installed or not on Android device(无法确定 Android 设备上是否安装了 Google Play 商店)
本文介绍了无法确定 Android 设备上是否安装了 Google Play 商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是检查设备上已安装软件包的简单问题...在我将操作系统升级到 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 商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)