Reputation: 12684
Whether the app installed is called Google Play or Market, the package name is the same com.android.vending
.
I need to be able to detect whether the app is Google Play or Market, I've checked in PackageInfo and nothing except versionCode
and versionName
can be of help.
Does anyone know what the first versionCode
was or versionName
was for Google Play app?
If anyone knows any other way of detecting this let me know.
Upvotes: 7
Views: 6879
Reputation: 1546
In my App I check possibility to open play store before fire it like:
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
}
}
Upvotes: 2
Reputation: 5391
You can use this simple piece of code, its easy and to the point with a consideration for not re-inventing the wheel using 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;
}
}
This will require you to add this to your dependencies:
compile 'com.google.android.gms:play-services-base:[PLAY_SERVICES_VERSION]'
Latest play-services
version is now: 10.0.1
Upvotes: 1
Reputation: 24039
This is probably a better example as it allows for status' where the user can do something about it i.e re-auth or update. Based on the code in the GCM client example project:
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;
}
Upvotes: 0
Reputation: 2913
You can also try this much simplified solution:
public boolean isGooglePlayAvailable() {
boolean googlePlayStoreInstalled;
int val= GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationActivity.this);
googlePlayStoreInstalled = val == ConnectionResult.SUCCESS;
return googlePlayStoreInstalled;
}
Upvotes: 2
Reputation: 12684
I figured out how to check the application label. I was using the debugger to see what all was being returned in packageInfo
that's why I didn't see it initially.
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;
}
Upvotes: 5