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;
}
}
}
Since I cannot stand my own stupidity, and I love to lecture people about how amazing exceptions are and how stupid it is to ignore them, I found that I have done it as well, although not in the same context I lecture people...
Following is the proper code to check whether an application is installed on your device:
protected final boolean isPackageInstalled(String packageName) {
try {
application.getPackageManager().getPackageInfo(packageName, 0);
} catch (NameNotFoundException e) {
return false;
}
return true;
}
As you can see, I've ignored the fact there is a method which throws an exception when it could not find the requested package, which is so dumb of me.So now I wrapped the method with a boolean returning method, and do use the exception to determine whether the application package is installed or not.
No comments:
Post a Comment