如何获取未安装apk的包名,应用名称,icon

转载:https://blog.csdn.net/qiaowe/article/details/79100241

/**
 * 获取未安装 apk 的 icon
 *
 * @param context
 * @param apkPath  是apk的文件地址
 * @return
 */
public Drawable getApkIcon(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            return appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
            Log.e("ApkIconLoader", e.toString());
        }
    }
    return null;
}

/**
 * 获取未安装 apk 的 应用名称
 *
 * @param context
 * @param apkPath  是apk的文件地址
 * @return
 */

public String getApkName(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        //关键在下面这两行数据上吗  这两个等于
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        String label = appInfo.loadLabel(pm).toString();
        try {
            return label;
        } catch (OutOfMemoryError e) {
            Log.e("ApkIconLoader", e.toString());
        }
    }
    return null;
}

你可能感兴趣的:(如何获取未安装apk的包名,应用名称,icon)