is Apk File Installed - Android android.content.pm

Android examples for android.content.pm:Apk Install

Description

is Apk File Installed

Demo Code

import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {

  public static boolean isApkFileInstalled(Context context,
      String archiveFilePath) {/*  ww w.  j ava 2 s .c om*/
    if (null == archiveFilePath) {
      throw new IllegalArgumentException("archiveFilePath cannot be null");
    }
    PackageInfo info = getPackageInfo(context, archiveFilePath);
    if (info == null) {
      return false;
    }
    return isApkInstalled(context, info.packageName);
  }

  public static PackageInfo getPackageInfo(Context context,
      String archiveFilePath) {
    PackageManager pm = context.getPackageManager();
    return pm.getPackageArchiveInfo(archiveFilePath,
        PackageManager.GET_ACTIVITIES);
  }

  public static boolean isApkInstalled(Context context, String packageName) {
    PackageManager pkgManager = context.getPackageManager();

    List<PackageInfo> pkgs = pkgManager
        .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (PackageInfo pkg : pkgs) {
      if (packageName.equals(pkg.packageName)) {
        return true;
      }
    }
    return false;
  }

}

Related Tutorials