如何以编程方式在android上运行应用程序图标

时间:2013-03-22 10:21:48

标签: 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);
                    }

2 个答案:

答案 0 :(得分:12)

只需替换此行

ico = pm.getApplicationIcon(pName);

到这个

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

已编辑完整代码:

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

答案 1 :(得分:0)

要检索应用图标,您可以使用getApplicationIcon() PackageManger 方法。

在您的情况下,由于您正在检索正在运行的应用程序的图标,因此您可以执行以下操作:

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() 永远不会返回 null ,而是空列表最多。