可获取APP的图标、名称、包名、安装路径(SD卡or内存)、应用程序类型(系统应用or用户应用)等信息
- public class AppInfoUtils {
- public static List<AppInfoModel> getAppInfos(Context context){
- List<AppInfoModel> appInfos = new ArrayList<AppInfoModel>();
-
- PackageManager packageManager = context.getPackageManager();
- List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
-
- for (PackageInfo packageInfo : installedPackages) {
- //packageInfo.applicationInfo就相当于对应AndroidMenifest中的信息
-
- //获取应用程序的图标
- Drawable appIcon = packageInfo.applicationInfo.loadIcon(packageManager);
- //获取应用程序的名称
- String appName = (String) packageInfo.applicationInfo.loadLabel(packageManager);
- //获取应用程序的包名
- String packageName = packageInfo.packageName;
-
- //获取应用程序的资源文件大小
- String sourceDir = packageInfo.applicationInfo.sourceDir;
- File resourceFile = new File(sourceDir);
- long appSize = resourceFile.length();
-
- //App的类型
- //int appType = sourceDir.startsWith("/data/app/") ? AppInfoModel.APP_TYPE_USER : AppInfoModel.APP_TYPE_SYSTEM;
-
- int appType = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? AppInfoModel.APP_TYPE_SYSTEM : AppInfoModel.APP_TYPE_USER;
- int appPositionType = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE )!= 0 ? AppInfoModel.APP_TYPE_POSTION_SD : AppInfoModel.APP_TYPE_POSTION_SD;
- AppInfoModel model = new AppInfoModel();
- model.setAppName(appName);
- model.setApkPackageName(packageName);
- model.setApkSize(appSize);
- model.setAppIcon(appIcon);
- model.setAppPositionType(appPositionType);
- model.setAppType(appType);
-
- appInfos.add(model);
- }
-
- return appInfos;
- }
- }
复制代码
|
|