'How can I find if a particular package exists on my Android device?

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



Solution 1:[1]

Call any of the below method with the package name.

import 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 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;
 }

Solution 2:[2]

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

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;
}

Solution 3:[3]

Kotlin

fun isPackageExist(context: Context, target: String): Boolean {
     return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}

Edit: Extension Function

fun Context.isPackageExist(target: String): Boolean {
     return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}

Solution 4:[4]

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;

Solution 5:[5]

We can check like this:

 if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
            if (Constant.isNetworkConnected(Activity.this)) {
                 //Your Intent 
            } else {
                Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
            }
        }   else
            {
                Constant.showDialog(Activity.this,"Please install the webview");
            }
    }

Make method for package check ! this credit goes to "Kavi" https://stackoverflow.com/a/30708227/6209105

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;

}

Solution 6:[6]

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.

Solution 7:[7]

If you just want to use adb:

adb shell "pm list packages"|cut -f 2 -d ":"

it will list all installed packages.

Solution 8:[8]

You can use pm.getPackageUid() instead of iterating over the pm.getInstalledApplications()

 boolean isPackageInstalled;
 PackageManager pm = getPackageManager();   
 int flags = 0; 
        try 
        {   
              pm.getPackageUid(packageName,flags);               
              isPackageInstalled = true;    
        }   
        catch (final PackageManager.NameNotFoundException nnfe) 
        {   
            isPackageInstalled = false; 
        }                   
 return isPackageInstalled;

Solution 9:[9]

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);
        }
        closeQuietly(bufferedReader);
        process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeQuietly(bufferedReader);
    }
    return result;
}

public static void closeQuietly(final Closeable closeable) {
    if (closeable == null)
        return;
    try {
        closeable.close();
    } catch (final IOException e) {
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zavidovych
Solution 2 Kavi
Solution 3
Solution 4 user3078812
Solution 5 A Pars
Solution 6 inazaruk
Solution 7
Solution 8 Gowtham
Solution 9 android developer