2013-03-22 95 views
2

下面這裏是我的代碼,但我得到了所有正在運行的應用程序默認的Android啓動器圖標:如何讓Android上運行的應用程序圖標編程

PackageManager pm = getPackageManager(); 

      ActivityManager am1 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 

      List<RunningTaskInfo> processes = am1.getRunningTasks(Integer.MAX_VALUE); 

       if (processes != null) { 
        for (int k = 0; k < processes.size(); k++) { 
         // String pkgName = app.getPackageName(); 
         String packageName = processes.get(k).topActivity 
           .getPackageName(); 
         Drawable ico = null; 
         try 
         { 
         String pName = (String) pm.getApplicationLabel(pm 
           .getApplicationInfo(packageName, 
             PackageManager.GET_META_DATA)); 

          ico = pm.getApplicationIcon(pName); 

         } 
         catch (NameNotFoundException e) 
         { 
          Log.e("ERROR", "Unable to find icon for package '" 
            + packageName + "': " + e.getMessage()); 
         } 
         icons.put(processes.get(k).topActivity.getPackageName(),ico); 
        } 
+0

請注意,getRunningTasks()將永遠不會返回null,但最多隻能爲空列表。閱讀下面的答案。 – 2013-09-18 13:33:13

回答

10

只需更換這行

ico = pm.getApplicationIcon(pName); 

ico = getApplicationInfo().loadIcon(getPackageManager()); 

EDITED全碼:

public void getAllICONS() { 

    PackageManager pm = getPackageManager(); 

    ActivityManager am1 = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 

    List<RunningTaskInfo> processes = am1 
      .getRunningTasks(Integer.MAX_VALUE); 

    if (processes != null) { 
     for (int k = 0; k < 5; k++) { 
      // String pkgName = app.getPackageName(); 
      String packageName = processes.get(k).topActivity 
        .getPackageName(); 
      Log.e("packageName-->", "" + packageName); 
      Drawable ico = null; 
      try { 
       String pName = (String) pm.getApplicationLabel(pm 
         .getApplicationInfo(packageName, 
           PackageManager.GET_META_DATA)); 
       name.add("" + pName); 
       ApplicationInfo a = pm.getApplicationInfo(packageName, 
         PackageManager.GET_META_DATA); 
       ico = getPackageManager().getApplicationIcon(
         processes.get(k).topActivity.getPackageName()); 
       getPackageManager(); 
       Log.e("ico-->", "" + ico); 

      } catch (NameNotFoundException e) { 
       Log.e("ERROR", "Unable to find icon for package '" 
         + packageName + "': " + e.getMessage()); 
      } 
      // icons.put(processes.get(k).topActivity.getPackageName(),ico); 
      icons.add(ico); 

     } 
    } 
} 

上面的代碼是顯示你喜歡的圖標:

enter image description here

+0

它顯示所有應用程序的默認圖標爲什麼 – 2013-03-25 10:20:33

+0

嗨@RajivGaikwad:請檢查此:http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html – 2013-03-28 06:53:11

+0

首先告訴我你需要刪除什麼類型的數據?運行應用程序或任何其他數據(如表單sqlite數據庫)?? – 2013-03-28 08:33:58

0

要檢索的應用程序圖標,你可以使用PackageMangergetApplicationIcon()方法。

在你的情況,因爲你是retriving正在運行的應用程序的圖標,你可以這樣做:

final ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE); 
final PackageManager pm = getPackageManager(); 
List<ActivityManager.RunningTaskInfo> runningTasks; 

try { 
    runningTasks = am.getRunningTasks(100); 
} 
catch (SecurityException e) { 
    runningTasks = new ArrayList<ActivityManager.RunningTaskInfo>(); 
    //e.printStackTrace(); 
} 

Drawable icon; 

for (ActivityManager.RunningTaskInfo task : runningTasks) { 
    final String packageName = task.topActivity.getPackageName(); 

    try { 
      icon = pm.getApplicationIcon(packageName); 
    } 
    catch (PackageManager.NameNotFoundException e) { 
      //e.printStackTrace(); 
    } 
} 

否則,甚至更好,你可以使用其他實施getApplicationIcon(),這樣的:

ApplicationInfo ai; 

try { 
    ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); 
} catch (PackageManager.NameNotFoundException e) { 
    ai = null; 
    //e.printStackTrace(); 
} 

if (ai != null) { 
    icon = pm.getApplicationIcon(ai); 
} 

PS:在任何情況下,請注意getRunningTasks()永遠[R eturn null,但是最多爲空列表

相關問題