无论安装的应用程序名为Google Play还是Market,包名都是相同的com.android.vending
。
我需要能够检测应用程序是Google Play还是Market,我已经检查了PackageInfo,除了versionCode
和versionName
之外没有什么可以帮助。
有谁知道第一个versionCode
是什么或versionName
是谷歌Play应用程序?
如果有人知道任何其他检测方法请告诉我
我知道如何检查应用程序标签。我正在使用调试器来查看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 store的可能性,然后像这样:
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;
}