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