程序大概分成三个部分:
1.获取手机已安装的所有应用package的信息(其中包括用户自己安装的,还有系统自带的);
2.滤除系统自带应用;
3.通过列表显示出应用程序的图标(icon),和其他文字信息(应用名称,包名称package name,版本号等等)
首先,我们定义一个数据结构,来保存应用程序信息(icon,name,packageName,versionName,versionCode,等)
[java]
public class AppInfo {
public String appName="";
public String packageName="";
public String versionName="";
public int versionCode=0;
public Drawable appIcon=null;
public void print()
{
Log.v("app","Name:"+appName+" Package:"+packageName);
Log.v("app","Name:"+appName+" versionName:"+versionName);
Log.v("app","Name:"+appName+" versionCode:"+versionCode);
}
}
然后我们通过PackageManager 来获取已安装的应用包信息。
[java]
ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据 List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo = packages.get(i);
AppInfo tmpInfo = new AppInfo();
tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName = packageInfo.packageName;
tmpInfo.versionName = packageInfo.versionName;
tmpInfo.versionCode = packageInfo.versionCode;
tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(getPackageManager());
appList.add(tmpInfo);
}//好啦 这下手机上安装的应用数据都存在appList里了。
[java]
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)
{
//非系统应用
}
else{ //系统应用
}
[java]
ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo = packages.get(i);
AppInfo tmpInfo = new AppInfo();
tmpInfo.appName = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName = packageInfo.packageName;
tmpInfo.versionName = packageInfo.versionName;
tmpInfo.versionCode = packageInfo.versionCode;
tmpInfo.appIcon = packageInfo.applicationInfo.loadIcon(getPackageManager());
//Only display the non-system app info
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)
{
appList.add(tmpInfo);//如果非系统应用,则添加至appList
}
}