English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method for obtaining information about installed applications in the system for Android development

This example explains the method for Android developers to obtain information about installed applications in the system. Shared for everyone's reference, as follows:

public class AppInfoParser {
  private static String tag = "AppInfoParser";
  public static List<AppInfo> getAppInfos(Context context) {
   //First obtain the package manager
    PackageManager packageManager = context.getPackageManager();
    //Obtain all installed packages
    List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
    ArrayList<AppInfo> appInfos = new ArrayList<>();
    for (PackageInfo installedPackage : installedPackages) {
      AppInfo appInfo = new AppInfo();
      //Package name
      String packageName = installedPackage.packageName;
      appInfo.setPackageName(packageName);
      //Obtain the icon
      Drawable icon = installedPackage.applicationInfo.loadIcon(packageManager);
      appInfo.setIcon(icon);
      //Obtain the name of the application
      String appName = installedPackage.applicationInfo.loadLabel(packageManager).toString();
      appInfo.setAppName(appName);
      //Obtain the path of the installed package
      String sourceDir = installedPackage.applicationInfo.sourceDir;
      File file = new File(sourceDir);
      //Obtain the size of the installed apk
      long apkSize = file.length();
      //Format the size of the apk
      appInfo.setApkSize(Formatter.formatFileSize(context, apkSize));
      int flags = installedPackage.applicationInfo.flags;
      //Determine if the current app is a system app
      if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
        //Then it is a system app
        appInfo.setUserApp(false);
      } else {
        //Then it is a user app
        appInfo.setUserApp(true);
      }
      if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
       //Then the currently installed is on the SD card
        appInfo.setSD(true);
      } else {
        //Then it is the phone memory
        appInfo.setSD(false);
      }
      appInfos.add(appInfo);
    }
    return appInfos;
  }
}
public class AppInfo {
  //Application icon
  private Drawable icon;
  //Application name
  private String appName;
  //Application size
  private String apkSize;
  //Represents a user application
  private boolean isUserApp;
  //The storage location.
  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 +
        };
  }
}

Readers who are interested in more about Android-related content can check the special topics on this site: 'Android Development Tutorial for Beginners and Advanced Users', 'Summary of Android Debugging Skills and Common Problem Solving Methods', 'Summary of Usage of Android Basic Components', 'Summary of Android View View Skills', 'Summary of Android Layout Layout Skills', and 'Summary of Android Control Usage'

I hope the content described in this article will be helpful to everyone's Android program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like