Android How to - Check if a package exists








Question

We would like to know how to check if a package exists.

Answer

//w w w  .j  a  v a2  s  .com
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class Main {
  public static boolean exist(Context context, String packageName) {
    if (null == packageName || "".equals(packageName)) {
      throw new IllegalArgumentException(
          "Package name cannot be null or empty !");
    }
    try {
      ApplicationInfo info = context.getPackageManager().getApplicationInfo(
          packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
      return null != info;
    } catch (Exception e) {
      return false;
    }
  }
}