[英]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.