安卓(Android) 怎么判断是否安装了apk文件

2019年10月17日21:38:55安卓(Android) 怎么判断是否安装了apk文件已关闭评论 403 1228字阅读4分5秒

1.简介

安卓(Android) 怎么判断是否安装了apk文件?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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

    public static List<PackageInfo> getInstalledPackages(Context context) {
        PackageManager pkgManager = context.getPackageManager();
        return pkgManager
                .getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
    }
}

 
继续阅读
编程十万个怎么办