Try this:
private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException
was thrown, it means that no package with that name is installed, so we return false
.
Note that we pass in a PackageManager
instead of a Context
, so that the method is slightly more flexibly usable and doesn't violate the law of Demeter. You can use the method without access to a Context
instance, as long as you have a PackageManager
instance.
Use it like this:
public void someMethod() {
// ...
PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled("com.somepackage.name", pm);
// ...
}
Note: From Android 11 (API 30), you might need to declare <queries>
in your manifest, depending on what package you're looking for. Check out the docs for more info.
Since Android 11 (API level 30), most user-installed apps are not visible by default. In your manifest, you must statically declare which apps you are going to get info about, as in the following:
<manifest>
<queries>
<!-- Explicit apps you know in advance about: -->
<package android:name="com.example.this.app"/>
<package android:name="com.example.this.other.app"/>
</queries>
...
</manifest>
Then, @RobinKanters' answer works:
private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
// ...
// This will return true on Android 11 if the app is installed,
// since we declared it above in the manifest.
isPackageInstalled("com.example.this.app", pm);
// This will return false on Android 11 even if the app is installed:
isPackageInstalled("another.random.app", pm);
Learn more here:
Robin Kanters' answer is right, but it does check for installed apps regardless of their enabled or disabled state.
We all know an app can be installed but disabled by the user, therefore unusable.
This checks for installed AND enabled apps:
public static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
return packageManager.getApplicationInfo(packageName, 0).enabled;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
You can put this method in a class like Utils
and call it everywhere using:
boolean isInstalled = Utils.isPackageInstalled("com.package.name", context.getPackageManager())
Faster solution:
private boolean isPackageInstalled(String packagename, PackageManager packageManager) {
try {
packageManager.getPackageGids(packagename);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
getPackageGids
is less expensive from getPackageInfo
, so it work faster.
Run 10000 on API 15
Exists pkg:
getPackageInfo: nanoTime = 930000000
getPackageGids: nanoTime = 350000000
Not exists pkg:
getPackageInfo: nanoTime = 420000000
getPackageGids: nanoTime = 380000000
Run 10000 on API 17
Exists pkg:
getPackageInfo: nanoTime = 2942745517
getPackageGids: nanoTime = 2443716170
Not exists pkg:
getPackageInfo: nanoTime = 2467565849
getPackageGids: nanoTime = 2479833890
Run 10000 on API 22
Exists pkg:
getPackageInfo: nanoTime = 4596551615
getPackageGids: nanoTime = 1864970154
Not exists pkg:
getPackageInfo: nanoTime = 3830033616
getPackageGids: nanoTime = 3789230769
Run 10000 on API 25
Exists pkg:
getPackageInfo: nanoTime = 3436647394
getPackageGids: nanoTime = 2876970397
Not exists pkg:
getPackageInfo: nanoTime = 3252946114
getPackageGids: nanoTime = 3117544269
Note: This will not work in some virtual spaces. They can violate the Android API and always return an array, even if there is no application with that package name.
In this case, use getPackageInfo
.
Try this:
public static boolean isAvailable(Context ctx, Intent intent) {
final PackageManager mgr = ctx.getPackageManager();
List<ResolveInfo> list =
mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With