3

Below here is my code but i am getting default android launcher icon for all running applications:

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);
                    }
Ankur
  • 5,086
  • 19
  • 37
  • 62
Rajiv Gaikwad
  • 105
  • 1
  • 2
  • 13

2 Answers2

15

just replace this line

ico = pm.getApplicationIcon(pName);

to this

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

EDITED full code :

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);

        }
    }
}

above code is show you icons like:

enter image description here

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • hi @RajivGaikwad: please check this : http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html – Dhaval Parmar Mar 28 '13 at 06:53
  • first tell me what type of data you have to delete?? running application?? or any other data (like form sqlite db)?? – Dhaval Parmar Mar 28 '13 at 08:33
  • then also same process. i think you have checked my code here: http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html ok then you get selected position. m i right?? if yes then on deleted button click item you have to make for loop like for(int i = 0; i < posSel.size(); i++){adapter.remove(adapter.getItem(i)); } after that call notifyDataSetChanged(); – Dhaval Parmar Mar 28 '13 at 10:09
  • In your given solution i have add the checkbox in scanrow1.xml and in activitymain.xml added the kill button .onclick of this button i want to delete these all checked row....Thanks – Rajiv Gaikwad Mar 28 '13 at 10:15
  • i thing you hace created that object in listadaptert a = new listadaptert(this); list.setadapter(a); ok... so use that a.remove(a.getitem(i)); – Dhaval Parmar Mar 28 '13 at 10:30
0

To retrieve the app icon, you can use the getApplicationIcon() method of the PackageManger.

In your case, since you are retriving the icon of the running apps, you can do something like:

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();
    }
}

Otherwise, even better, you may use the other implementation of getApplicationIcon(), thus:

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: In any case, please note that getRunningTasks() will never return null, but an empty list at most.

Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37