首先要建立一个javabean用来存储应用程序信息,包括名称,包名,图标,存储位置,是不是系统程序
然后用PackageManager来获取
package com.itheima.bean; import android.graphics.drawable.Drawable; public class AppInfo { private Drawable icon; private String name; private String pageName; /** * true是在内部 false在sdcard */ private boolean isRow; /** * true 是用户程序 false 是系统程序 */ private boolean isUser; public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPageName() { return pageName; } public void setPageName(String pageName) { this.pageName = pageName; } public boolean isRow() { return isRow; } public void setRow(boolean isRow) { this.isRow = isRow; } public boolean isUser() { return isUser; } public void setUser(boolean isUser) { this.isUser = isUser; } @Override public String toString() { return "AppInfo [name=" + name + ", pageName=" + pageName + ", isRow=" + isRow + ", isUser=" + isUser + "]"; } }
具体获取的实现
package com.itheima.engine; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import com.itheima.bean.AppInfo; public class AppInfoProvider { public static List<AppInfo> getAppMessage(Context context) { List<AppInfo> list = new ArrayList<AppInfo>(); PackageManager packageManager = context.getPackageManager(); List<PackageInfo> installedPackages = packageManager .getInstalledPackages(0); for (int i = 0; i < installedPackages.size(); i++) { AppInfo app = new AppInfo(); //包名 app.setPageName(installedPackages.get(i).packageName); //程序图标 app.setIcon(installedPackages.get(i).applicationInfo .loadIcon(packageManager)); //程序名称 app.setName(installedPackages.get(i).applicationInfo.loadLabel( packageManager).toString()); //应用程序的标示 int flag = installedPackages.get(i).applicationInfo.flags; //判断是不是用户程序 if ((flag & installedPackages.get(i).applicationInfo.FLAG_SYSTEM) == 0) { //应用程序 app.setUser(true); } else { //系统程序 app.setUser(false); } //判断是内部存储还是外部存储 if ((flag & installedPackages.get(i).applicationInfo.FLAG_EXTERNAL_STORAGE) == 0) { //内部 app.setRow(true); } else { //外部 app.setRow(false); } list.add(app); } return list; } }