Android获取手机已安装apk文件信息 发表于 2015-07-29 | 分类于 Android | 阅读次数: 1234567891011121314Intent intent = new Intent(Intent.ACTION_MAIN, null);intent.addCategory(Intent.CATEGORY_LAUNCHER);List<AppInfo> applicationList = new ArrayList<>();List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);for (ResolveInfo resolveInfo : resolveInfoList) {AppInfo appInfo = new AppInfo(MainActivity.this, resolveInfo);PackageInfo packageInfo = appInfo.getPackageInfo();//判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,// 该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用if ((packageInfo.applicationInfo.flags & packageInfo.applicationInfo.FLAG_SYSTEM) <= 0) {// customs applicationsapplicationList.add(appInfo); }} 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172package com.wuxiaolong.apksample;import android.annotation.SuppressLint;import android.content.ComponentName;import android.content.Context;import android.content.pm.PackageInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.content.pm.ResolveInfo;import android.content.res.AssetManager;import android.content.res.Configuration;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.util.DisplayMetrics;import java.util.Locale;public class AppInfo implements Comparable<Object> { private Context context; private ResolveInfo resolveInfo; private ComponentName componentName = null; private PackageInfo packageInfo = null; private Drawable icon = null; private String name = null; public AppInfo(Context context, ResolveInfo ri) { this.context = context; this.resolveInfo = ri; this.componentName = new ComponentName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name); try { packageInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0); } catch (NameNotFoundException e) { } } public String getName() { if (name != null) { return name; } else { try { return getNameFromResolveInfo(resolveInfo); } catch (NameNotFoundException e) { return getPackageName(); } } } public String getActivityName() { return resolveInfo.activityInfo.name; } public String getPackageName() { return resolveInfo.activityInfo.packageName; } public ComponentName getComponentName() { return componentName; } public String getComponentInfo() { if (getComponentName() != null) { return getComponentName().toString(); } else { return ""; } } public ResolveInfo getResolveInfo() { return resolveInfo; } public PackageInfo getPackageInfo() { return packageInfo; } public String getVersionName() { PackageInfo pi = getPackageInfo(); if (pi != null) { return pi.versionName; } else { return ""; } } public int getVersionCode() { PackageInfo pi = getPackageInfo(); if (pi != null) { return pi.versionCode; } else { return 0; } } public Drawable getIcon() { if (icon == null) { icon = getResolveInfo().loadIcon(context.getPackageManager()); /* Drawable dr = getResolveInfo().loadIcon(ctx.getPackageManager()); Bitmap bitmap = ((BitmapDrawable) dr).getBitmap(); icon = new BitmapDrawable(ctx.getResources(), AppHelper.getResizedBitmap(bitmap, 144, 144)); */ } return icon; } @SuppressLint("NewApi") public long getFirstInstallTime() { PackageInfo pi = getPackageInfo(); if (pi != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { return pi.firstInstallTime; } else { return 0; } } @SuppressLint("NewApi") public long getLastUpdateTime() { PackageInfo pi = getPackageInfo(); if (pi != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) { return pi.lastUpdateTime; } else { return 0; } } @Override public int compareTo(Object o) { AppInfo f = (AppInfo) o; return getName().compareTo(f.getName()); } @Override public String toString() { return getName(); } /** * Helper method to get an applications name! * * @param ri * @return * @throws NameNotFoundException */ public String getNameFromResolveInfo(ResolveInfo ri) throws NameNotFoundException { String name = ri.resolvePackageName; if (ri.activityInfo != null) { Resources res = context.getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo); Resources engRes = getEnglishRessources(res); if (ri.activityInfo.labelRes != 0) { name = engRes.getString(ri.activityInfo.labelRes); if (name == null || name.equals("")) { name = res.getString(ri.activityInfo.labelRes); } } else { name = ri.activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString(); } } return name; } public Resources getEnglishRessources(Resources standardResources) { AssetManager assets = standardResources.getAssets(); DisplayMetrics metrics = standardResources.getDisplayMetrics(); Configuration config = new Configuration(standardResources.getConfiguration()); config.locale = Locale.US; return new Resources(assets, metrics, config); }} 参考博客 【Android】获取手机中已安装apk文件信息(PackageInfo、ResolveInfo)(应用图片、应用名、包名等) 联系作者 我的微信公众号:吴小龙同学,欢迎关注交流,公号回复关键字「1024」有惊喜哦。