时间:2022-05-18 08:42:39 | 栏目:Android代码 | 点击:次
本文实例讲述了Android开发获取系统中已安装程序信息的方法。分享给大家供大家参考,具体如下:
public class AppInfoParser { private static String tag = "AppInfoParser"; public static List<AppInfo> getAppInfos(Context context){ //首先获取到包的管理者 PackageManager packageManager = context.getPackageManager(); //获取到所有的安装包 List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); ArrayList<AppInfo> appInfos = new ArrayList<>(); for (PackageInfo installedPackage : installedPackages) { AppInfo appInfo = new AppInfo(); //程序包名 String packageName = installedPackage.packageName; appInfo.setPackageName(packageName); //获取到图标 Drawable icon = installedPackage.applicationInfo.loadIcon(packageManager); appInfo.setIcon(icon); //获取到应用的名字 String appName = installedPackage.applicationInfo.loadLabel(packageManager).toString(); appInfo.setAppName(appName); //获取到安装包的路径 String sourceDir = installedPackage.applicationInfo.sourceDir; File file = new File(sourceDir); //获取到安装apk的大小 long apkSize = file.length(); //格式化apk的大小 appInfo.setApkSize(Formatter.formatFileSize(context,apkSize)); int flags = installedPackage.applicationInfo.flags; //判断当前是否是系统app if((flags & ApplicationInfo.FLAG_SYSTEM) !=0){ //那么就是系统app appInfo.setUserApp(false); }else{ //那么就是用户app appInfo.setUserApp(true); } if((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0){ //那么当前安装的就是sd卡 appInfo.setSD(true); }else{ //那么就是手机内存 appInfo.setSD(false); } appInfos.add(appInfo); } return appInfos; } }
public class AppInfo { //应用图标 private Drawable icon; //应用的名字 private String appName; //应用程序的大小 private String apkSize; //表示用户程序 private boolean isUserApp; //存储的位置. private boolean isSD; private String packageName; public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getApkSize() { return apkSize; } public void setApkSize(String apkSize) { this.apkSize = apkSize; } public boolean isUserApp() { return isUserApp; } public void setUserApp(boolean isUserApp) { this.isUserApp = isUserApp; } public boolean isSD() { return isSD; } public void setSD(boolean isSD) { this.isSD = isSD; } @Override public String toString() { return "AppInfo{" + "appName='" + appName + '\'' + ", apkSize='" + apkSize + '\'' + ", isUserApp=" + isUserApp + ", isSD=" + isSD + '}'; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。