无论安装的应用是 Google Play 还是 Market,包名都是一样的com.android.vending。
我需要能够检测应用程序是 Google Play 还是 Market,我已经检查了 PackageInfo,除了可以提供帮助之外什么都versionCode没有versionName。
有谁知道Google Play 应用程序的第一个versionCode是什么?versionName
如果有人知道任何其他检测方法,请告诉我。
无论安装的应用是 Google Play 还是 Market,包名都是一样的com.android.vending。
我需要能够检测应用程序是 Google Play 还是 Market,我已经检查了 PackageInfo,除了可以提供帮助之外什么都versionCode没有versionName。
有谁知道Google Play 应用程序的第一个versionCode是什么?versionName
如果有人知道任何其他检测方法,请告诉我。
我想出了如何检查应用程序标签。我正在使用调试器查看所有返回的内容,packageInfo这就是为什么我最初没有看到它。
public static boolean isGooglePlayInstalled(Context context) {
PackageManager pm = context.getPackageManager();
boolean app_installed = false;
try
{
PackageInfo info = pm.getPackageInfo("com.android.vending", PackageManager.GET_ACTIVITIES);
String label = (String) info.applicationInfo.loadLabel(pm);
app_installed = (label != null && !label.equals("Market"));
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed;
}
您也可以尝试这个非常简化的解决方案:
public boolean isGooglePlayAvailable() {
boolean googlePlayStoreInstalled;
int val= GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationActivity.this);
googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
return googlePlayStoreInstalled;
}
在我的应用程序中,我检查在开火之前打开 Play 商店的可能性,例如:
public static boolean isResolveActivity(Intent intent) {
return App.getInstance().getPackageManager().resolveActivity(intent, PackageManager.GET_RESOLVED_FILTER) != null;
}
public void isResolveActivity(String appPackage) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackage));
if(isResolveActivity(intent)){
...open intent
}
}
您可以使用这段简单的代码,它既简单又切中要害,并考虑不使用以下方法重新发明轮子GooglePlayServicesUtil:
public static boolean isPlayStoreInstalled(Context context){
try {
context.getPackageManager()
.getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
这将要求您将其添加到您的依赖项中:
compile 'com.google.android.gms:play-services-base:[PLAY_SERVICES_VERSION]'
现在最新play-services版本是:10.0.1
这可能是一个更好的例子,因为它允许用户可以对其执行某些操作的状态,即重新验证或更新。基于GCM 客户端示例项目中的代码:
public static boolean checkPlayServices(Activity activity) {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(activity.getApplicationContext(), "This device is not supported.", Toast.LENGTH_LONG).show();
activity.finish();
}
return false;
}
return true;
}