import java.io.File;
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;
import android.os.SystemClock;
import com.home.bean.AppInfo;
/**
* 业务方法,用于获取系统里面的应用程序信息
* @author HQ
*/
public class AppInfoProvider {
/**
* 获取系统所有的应用程序信息集合
*
* @return
*/
public static List<AppInfo> getAllAppInfos(Context context) {
// PackageManager 包管理器 ,管理手机里面的应用程序信息
PackageManager pm = context.getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);
List<AppInfo> appInfos = new ArrayList<AppInfo>();
for (PackageInfo packageInfo : list) {
AppInfo appInfo = new AppInfo();
// 包名
String packageName = packageInfo.packageName;
// 图标
Drawable appIcon = packageInfo.applicationInfo.loadIcon(pm);
// 应用名称
String appName = packageInfo.applicationInfo.loadLabel(pm)
.toString();
// 安装应用路径
String apkPath = packageInfo.applicationInfo.sourceDir;
// 应用程序的标记, flags, 可以是很多标记的一个组合.
int flags = packageInfo.applicationInfo.flags;
if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {// 系统应用
appInfo.setSystemApp(true);
} else {// 用户应用
appInfo.setSystemApp(false);
}
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
// 安装在sd卡上
appInfo.setInRom(false);
} else {
// 安装在手机内存中
appInfo.setInRom(true);
}
File file = new File(apkPath);
long appSize = file.length();
appInfo.setAppIcon(appIcon);
appInfo.setAppName(appName);
appInfo.setAppSize(appSize);
appInfo.setPackageName(packageName);
appInfos.add(appInfo);
}
SystemClock.sleep(2000);
return appInfos;
}
}
业务bean
package com.home.bean;
import android.graphics.drawable.Drawable;
/**
* 应用程序的业务bean 用来保存应用程序的信息
*
* @author HQ
*
*/
public class AppInfo {
/**
* 图标
*/
private Drawable appIcon;
/**
* 名称
*/
private String appName;
/**
* 包名
*/
private String packageName;
/**
* 大小
*/
private long appSize;
/**
* 是否安装在手机的内部空间
*/
private boolean inRom;
/**
* 是否是系统应用
*/
private boolean systemApp;
/**
* 安装路径
*
* @return
*/
private String apkPath;
public Drawable getAppIcon() {
return appIcon;
}
public void setAppIcon(Drawable appIcon) {
this.appIcon = appIcon;
}
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 long getAppSize() {
return appSize;
}
public void setAppSize(long appSize) {
this.appSize = appSize;
}
public boolean isInRom() {
return inRom;
}
public void setInRom(boolean inRom) {
this.inRom = inRom;
}
public boolean isSystemApp() {
return systemApp;
}
public void setSystemApp(boolean systemApp) {
this.systemApp = systemApp;
}
public String getApkPath() {
return apkPath;
}
public void setApkPath(String apkPath) {
this.apkPath = apkPath;
}
@Override
public String toString() {
return "AppInfo [appIcon=" + appIcon + ", appName=" + appName
+ ", packName=" + packageName + ", appSize=" + appSize + "]";
}
}