程序大概分成三個部分:
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
}
}