Android 开发零碎知识点积累(三) -- 判断应用是否存在(已安装)

315 阅读1分钟
原文链接: segmentfault.com

源码中加入如下代码即可判断某Apk是否安装:

public  boolean isPkgInstalled(Context context, String packageName) {
    if (packageName == null || "".equals(packageName)) {
        return false;
    }
    android.content.pm.ApplicationInfo info = null;
    try {
        info = context.getPackageManager().getApplicationInfo(packageName, 0);
         return info != null;
    } catch (NameNotFoundException e) {
         return false;
    }
}