Sunteți pe pagina 1din 6

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

44

How can I learn whether a particular package exists on my Android device?


android

android-applicationinfo

How can I learn whether a particular package or application - say, "com.android.abc" - exists on my Android device?

share

improve this question


brig
935 6 27 46
Hamid Shatu
7,240 3 15 34

5 Answers

95

Call any of the below method with the package name.

Asked
Jul 20 '11 at 8:07

Edited
Mar 5 '14 at 10:24

Order By

Votes

import com.android.content.pm.PackageManager;
// ...
public boolean isPackageExisted(String targetPackage){
List<ApplicationInfo> packages;
PackageManager pm;

pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage))
return true;
}
return false;

import com.android.content.pm.PackageManager;
public boolean isPackageExisted(String targetPackage){
PackageManager pm=getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}

share

improve this answer


Rasel
11.9k 2 25 44
MarSoft
413 5 9

Thanks Rasel, I was looking for this only. Thank U very much. brig Jul 20 '11 at 8:32

Answered
Jul 20 '11 at 8:18

Edited
Apr 22 '15 at 14:30

You are welcome Rasel Jul 20 '11 at 8:35


What do I have to import to get this to work? I get cannot resolve symbol 'PackageManager' error. Oscar Swanros Jun 4 '14 at 23:40
add a comment

share

You should use PackageManager's function called getInstalledPackages() to get the list of all installed packages and the search for the
one you are interested in. Note that package name is located in PackageInfo.packageName field.

improve this answer


inazaruk
52.4k 17 145 140

share

Intent intent = new Intent("com.google.zxing.client.android.SCAN");


PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;

improve this answer


user3078812
31 1

Answered
Jul 20 '11 at 8:15

Without using a try-catch block or iterating through a bunch of packages:

Answered
Dec 7 '13 at 23:57

public static boolean isPackageInstalled(Context context, String packageName) {


final PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return false;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}

share

improve this answer


Kavi
2,339 2 12 18

Answered
Jun 8 '15 at 11:40

Since some devices have reported that the "getInstalledPackages" can cause TransactionTooLargeException (check here, here and
here), I think you should also have a fallback like I did below.
This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.

public static List<String> getInstalledPackages(final Context context) {


List<String> result = new ArrayList<>();
final PackageManager pm = context.getPackageManager();
try {
List<PackageInfo> apps = pm.getInstalledPackages(0);
for (PackageInfo packageInfo : apps)
result.add(packageInfo.packageName);
return result;
} catch (Exception ignored) {
//we don't care why it didn't succeed. We'll do it using an alternative way instead
}
// use fallback:
BufferedReader bufferedReader = null;
try {
Process process = Runtime.getRuntime().exec("pm list packages");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
final String packageName = line.substring(line.indexOf(':') + 1);
result.add(packageName);
}

share

improve this answer


android developer
40.3k 52 254 471

Your Answer

Answered
Mar 21 at 7:52

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

S-ar putea să vă placă și