Android implements a method for obtaining a list of application related information

  • 2020-06-03 08:15:54
  • OfStack

Described in this article to get the Androdi mobile application list method, such as access to Android application software properties, size, and the application path, the application name, etc., to obtain a list of all installed Android applications, including those uninstalled, but there is no clear data application, at the same time to the application information, judge whether system application, it is a necessary has the function of the application manager.

The specific implementation code is as follows:


//AppInfoProvider.java
package com.xh.ui;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
/**
 *  The class name: AppInfoProvider 
 *  Class description: Gets information about the application 
 *  The founders: LXH 
 */
public class AppInfoProvider {
 private PackageManager packageManager;
 // To obtain 1 A package manager 
 public AppInfoProvider(Context context){
 packageManager = context.getPackageManager();
 }
 /**
 * Get all the application information in the system, 
 * And save the application information to list In the list. 
 **/ 
 public List<AppInfo> getAllApps(){
 List<AppInfo> list = new ArrayList<AppInfo>();
 AppInfo myAppInfo;
  // Get information about all installed applications, including those that were uninstalled but not purged  
 List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
 for(PackageInfo info:packageInfos){
  myAppInfo = new AppInfo();
  // To get the package name 
  String packageName = info.packageName;
  // Get information about the application  
  ApplicationInfo appInfo = info.applicationInfo;
  // Get the app icon 
  Drawable icon = appInfo.loadIcon(packageManager);
  // Get the application size 
  //long codesize = packageStats.codeSize;
  //Log.i("info", "-->"+codesize);
  // Get the name of the application 
  String appName = appInfo.loadLabel(packageManager).toString();
  myAppInfo.setPackageName(packageName);
  myAppInfo.setAppName(appName);
  myAppInfo.setIcon(icon);
  
  if(filterApp(appInfo)){
  myAppInfo.setSystemApp(false);
  }else{
  myAppInfo.setSystemApp(true);
  }
  list.add(myAppInfo);
 }
 return list;
 }
 /**
 * Judge a 1 Is an application an application of the system, 
 * If I'm going back true Otherwise return false . 
 */ 
 public boolean filterApp(ApplicationInfo info){
 // Some applications can be updated if the user downloads them 1 The application of the system to update the original, it is still the system application, this is the determination of this situation 
 if((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
  return true;
 }else if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0){// Determine if the system is applied 
  return true;
 }
 return false;
 }
}

The Java classes associated with the instance are as follows:


//AppInfo.java
package com.xh.ui;
import android.graphics.drawable.Drawable;
/**
 *  The class name: AppInfo 
 *  Class description: An application class that contains program-related properties 
 *  The founders: LXH 
 */
public class AppInfo {
 private Drawable icon;
 private String appName;
 private String packageName;
 private boolean isSystemApp;
 private long codesize;
 public long getCodesize() {
 return codesize;
 }
 public void setCodesize(long codesize) {
 this.codesize = codesize;
 }
 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 getPackageName() {
 return packageName;
 }
 public void setPackageName(String packageName) {
 this.packageName = packageName;
 }
 public boolean isSystemApp() {
 return isSystemApp;
 }
 public void setSystemApp(boolean isSystemApp) {
 this.isSystemApp = isSystemApp;
 }
}

Detailed notes are provided in the example, so that readers can improve and perfect the program according to their own project requirements on the basis of understanding the function of the program.


Related articles: