2014-05-14 57 views
-1

我在android.I新曾用下面的代碼如何在列表中獲取已安裝的軟件包名稱?

List<PackageInfo> packages = getPackageManager().getInstalledPackages(0); 
    for(PackageInfo pack : packages) 
    { 
     ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities; 
     Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities"); 
     if(activityInfo!=null) 
     { 
      for(int i=0; i<activityInfo.length; i++) 
      { 
       Log.i("PC",""+ activityInfo[i]); 
       myList = new ArrayList(); 
       myList.add(pack.packageName); 

      } 
      aplist = new ArrayList<String>(); 
      aplist.add(pack.packageName); 
      Toast.makeText(this, "list are  "+aplist, Toast.LENGTH_LONG).show(); 

     } 


    } 

檢索所有已安裝的應用程序包名稱的列表,但它得到的差異敬酒包名。我如何在一個列表中獲得所有包名?

+0

沒有它的不重複:/ – Maxwell

+0

你是什麼意思'它得到包裹名稱在比較敬酒。 – GrIsHu

+0

我需要將所有包名作爲列表 – Maxwell

回答

0

爲什麼在每次迭代中初始化apList和myList變量? 看一看修改後的代碼下面這應該解決您的問題:

aplist = new ArrayList<String>(); 
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0); 
for(PackageInfo pack : packages) 
{ 
    ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities; 
    Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities"); 
    if(activityInfo!=null) 
    { 
     for(int i=0; i<activityInfo.length; i++) 
     { 
      Log.i("PC",""+ activityInfo[i]); 

      if (myList != null) 
       myList = new ArrayList(); 

      myList.add(pack.packageName); 
     } 

     aplist.add(pack.packageName); 
    } 
} 
Toast.makeText(this, "list are  "+aplist, Toast.LENGTH_LONG).show(); 
+0

空指針異常 – Maxwell

+0

@Maxwell查看我的答案。 – GrIsHu

0

下面的輔助函數會檢索所有安裝的應用程序與應用程序的名稱,包裝名稱,版本號和-code以及圖標。方法getPackages()返回所有應用程序的ArrayList

class PkgInfo { 
    private String appname = ""; 
    private String pname = ""; 
    private String versionName = ""; 
    private int versionCode = 0; 
    private Drawable icon; 
    private void prettyPrint() { 
     Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode); 
    } 
} 

private ArrayList<PkgInfo> getPackages() { 
    ArrayList<PkgInfo> apps = getInstalledApps(false); /* false = no system packages */ 
    final int max = apps.size(); 
    for (int i=0; i<max; i++) { 
     apps.get(i).prettyPrint(); 
    } 
    return apps; 
} 

private ArrayList<PkgInfo> getInstalledApps(boolean getSysPackages) { 
    ArrayList<PkgInfo> res = new ArrayList<PkgInfo>();   
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); 
    for(int i=0;i<packs.size();i++) { 
     PackageInfo p = packs.get(i); 
     if ((!getSysPackages) && (p.versionName == null)) { 
      continue ; 
     } 
     PkgInfo newInfo = new PkgInfo(); 
     newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); 
     newInfo.pname = p.packageName; 
     newInfo.versionName = p.versionName; 
     newInfo.versionCode = p.versionCode; 
     newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); 
     res.add(newInfo); 
    } 
    return res; 
} 

希望這可以幫助你。

+0

如何調用getInstalledApps函數 – Maxwell

+0

就像'ArrayList details = getInstalledApps(true)'這會給你所有應用程序的細節,例如應用程序名稱,包名稱,圖標等。 – GrIsHu

+0

這將不會從設備獲得所有安裝的應用程序 – Maxwell

相關問題