Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

how to get running applications icon on android programmatically

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);
                    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...