2012-05-22 76 views
15

我想要獲取所有安裝的應用程序的圖標。我可以使用包管理器來獲取這些圖標嗎?它有任何功能嗎?或者以任何其他方式獲取位圖中所有已安裝應用程序的圖標?在android中獲取所有安裝的應用程序的圖標

謝謝!

+0

從Android 3.0開始,您可能希望獲得更大的啓動器圖標,以至於無法按照您所描述的方式進行操作。如果是這樣,也許我下面的問題的答案可以幫助你:http://stackoverflow.com/questions/4600740/getting-app-icon-in-android/16279240#16279240 –

回答

29
try { 
    String pkg = "com.app.my";//your package name 
    Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg); 
    imageView.setImageDrawable(icon); 
} catch (PackageManager.NameNotFoundException ne) { 

} 

檢查here瞭解更多詳情。

+0

完美的答案。我向你推薦了@Ponmalar –

3

試試這個方法:建立一個叫做PackageInformation類:

public class PackageInformation { 

    private Context mContext; 

    public PackageInformation(Context context) { 
     mContext = context; 
    } 

    class InfoObject { 
     public String appname = ""; 
     public String pname = ""; 
     public String versionName = ""; 
     public int versionCode = 0; 
     public Drawable icon; 


     public void InfoObjectAggregatePrint() { //not used yet 
      Log.v(appname, appname + "\t" + pname + "\t" + versionName + "\t" + versionCode); 
     } 

    } 

    private ArrayList <InfoObject> getPackages() { 
     ArrayList <InfoObject> apps = getInstalledApps(false); 
     final int max = apps.size(); 
     for (int i = 0; i < max; i++) { 
      apps.get(i).prettyPrint(); 
     } 
     return apps; 
    } 

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

} 

抱膝這個什麼地方,現在從你的工作Activity類訪問信息做到這一點:

PackageInformation androidPackagesInfo = new PackageInformation(this); 
ArrayList <InfoObject> appsData = androidPackagesInfo.getInstalledApps(true); 

for (InfoObject info: appsData) { 
    Toast.makeText(MainActivity.this, info.appname, 2).show(); 
    Drawable somedrawable = info.icon; 

} 
3

我發現它最簡單的方法:

private List<ResolveInfo> installedApps() { 
    final Intent main_intent = new Intent(Intent.ACTION_MAIN, null); 
    main_intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    return package_manager.queryIntentActivities(main_intent, 0); 
} 

現在要獲取圖標,請使用:

for(ResolveInfo ri : installedApps()) { 
    // to get drawable icon --> ri.loadIcon(package_manager) 
} 
相關問題