android 根据apk文件路径获取apk的信息 获取手机上安装的应用信息

/**
 * 根据apk文件路径获取应用程序图片Drawable
 *
 * @param apkPath
 * @return
 */
private static Drawable getApkIcon(Context context, String apkPath) {
    PackageInfo pi = context.getPackageManager().getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    if (pi != null) {
        ApplicationInfo appInfo = pi.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            return appInfo.loadIcon(context.getPackageManager());
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }
    return null;
}

/**
 * 根据apk文件路径获取APK版本号(versionCode)
 *
 * @param apkPath
 * @return
 */
private static int getVersionCode(Context context, String apkPath) {
    PackageInfo pi = context.getPackageManager().getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    int versionCode = 1;
    if (pi != null) {
        versionCode = pi.versionCode;
    }
    return versionCode;
}

/**
 * 根据apk文件路径获取APK的包名
 *
 * @param apkPath
 * @return
 */
private static String getPackageName(Context context, String apkPath) {
    PackageInfo pi = context.getPackageManager().getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    String packageName = null;
    if (pi != null) {
        packageName = pi.packageName;
    }
    return packageName;
}

 

 

 

    /**
     * 获取手机上安装过的apk列表
     *
     * @return
     */
    private List getPackages() {
        List list = new ArrayList<>();
        // 获取已经安装的所有应用, PackageInfo 系统类,包含应用信息
        List packages = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { //非系统应用
                // AppInfo 自定义类,包含应用信息
                String appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();//获取应用名称
                String packageName = packageInfo.packageName; //获取应用包名,可用于卸载和启动应用
                list.add(new TypeCheck(packageName, false));
//             String versionName=  packageInfo.versionName;//获取应用版本名
//             int versionCode=  packageInfo.versionCode;//获取应用版本号
//               packageInfo.applicationInfo.loadIcon(getPackageManager());//获取应用图标
            } else { // 系统应用

            }
        }
        return list;
    }

 

你可能感兴趣的:(笔记)