首页 > 系统 > Android > 正文

Android开发获取系统中已安装程序信息的方法

2019-10-22 18:19:17
字体:
来源:转载
供稿:网友

本文实例讲述了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开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表