21

这是检查设备上已安装软件包的简单问题......在我将操作系统升级到 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;
        }
    }
}

由于某些原因,更新后,我根本找不到表明应用程序已安装的包名称,尽管它在设备上,并且我可以访问市场。

包名变了吗?还是我看错了?

谢谢,

亚当。

更新:

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

protected final boolean isPackageInstalled(String packageName) {
    try {
        application.getPackageManager().getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        return false;
    }
    return true;
}
4

6 回答 6

31

请注意,这近 5 年的旧代码并不是最佳的,而且 Google 不喜欢您无缘无故地检查所有已安装的软件包。请检查其他答案。

包名已更改,现在为 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;
        }
    }
}
于 2012-05-11T12:47:28.100 回答
12

GooglePlayServices 有一个实用程序类,其中包含处理此问题的方法:

isGooglePlayServicesAvailable(Context).

它为设备上播放服务的状态提供适当的错误对话框。

API参考:

GoogleApiAvailability.isGooglePlayServicesAvailable(android.content.Context)

于 2013-05-06T19:56:57.673 回答
9

正如迈克尔在评论中所说,Google Play 服务与 Google Play 商店不同。使用它来确定您的设备上是否安装了 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;
    }
}
于 2016-02-10T15:39:00.980 回答
2

在大多数情况下,我们想知道是否安装了 Google Play 商店,以便在预加载一些应用页面的情况下启动它。

为什么我们不能这样做:

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.
}
于 2017-03-29T22:21:28.410 回答
0

使用此代码,您可以检查您的设备上是否安装了Google Play 服务。

int val=GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
            if(val==ConnectionResult.SUCCESS)
            {
                play_installed=true;
            }
            else
            {
                play_installed=false;
            }
于 2013-07-17T07:17:51.273 回答
0

检查是否已安装并激活 Google Play 商店:

科特林

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

爪哇

   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;
   }
于 2021-09-02T12:56:18.257 回答