Android How to - Check if package has installed








Question

We would like to know how to check if package has installed.

Answer

//from  w w w. j  av a  2  s  .c  o  m
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources.NotFoundException;

public class Main {
  public static boolean hasInstallApk(Context context, String PackageName)
      throws NotFoundException {
    boolean flag = true;
    PackageManager mPm = context.getPackageManager();
    PackageInfo pkgInfo = null;
    try {
      pkgInfo = mPm.getPackageInfo(PackageName,
          PackageManager.GET_UNINSTALLED_PACKAGES);
    } catch (NameNotFoundException e) {
      flag = false;
    } catch (Exception ex) {
      flag = false;
      if (pkgInfo == null) {
        throw new NotFoundException();
      }
    }
    return flag;
  }
}