如何通过运行任务包名来获得topActivity?

时间:2016-02-12 03:58:02

标签: android mobile task

我想从正在运行的任务包名称中加载图标

com.android.smspush.WapPushManager

2 个答案:

答案 0 :(得分:0)

您可以通过以下代码获得最佳运行应用程序:获得applicationInfo后,您可以从info获取图标。

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
    ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
    try {
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        info.processName, PackageManager.GET_META_DATA));
        Drawable ico = info.loadIcon(pm);  //  Icon of the application
        Log.w("LABEL", c.toString());
    } catch (Exception e) {
        // Name Not FOund Exception
    }
}

答案 1 :(得分:0)

public class AppIconDrawable {
    private HashMap<String, Drawable> drawables;

    private static AppIconDrawable sharedInstance = null;

    private AppIconDrawable(){
        drawables = new HashMap<>();
    }

    public static AppIconDrawable getSharedInstance(){
        if (sharedInstance == null)
            sharedInstance = new AppIconDrawable();

        return sharedInstance;
    }

    public void setDrawableForKey(String key, Drawable drawable){
        drawables.put(key, drawable);
    }

    public Drawable getDrawableForKey(String key){
        return drawables.get(key);
    }
}

要保存我用过的图标,其中rp.process = com.android.smspush.WapPushManager

PackageManager manager = getActivity().getPackageManager();
List<ActivityManager.RunningAppProcessInfo> listProcesses = manager.getRunningAppProcesses();

for (ActivityManager.RunningAppProcessInfo info : listProcesses) {

    try {
        ApplicationInfo appinfo = manager.getApplicationInfo(info.processName, PackageManager.GET_META_DATA);
        AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, manager.getApplicationIcon(appinfo));
    } catch (PackageManager.NameNotFoundException e) {
        AppIconDrawable.getSharedInstance().setDrawableForKey(info.processName, context.getResources().getDrawable(R.drawable.ic_android_default));
    }
}

要获取我使用过的图标,其中rp.process = com.android.smspush.WapPushManager

holder.imgApp.setImageDrawable(AppIconDrawable.getSharedInstance().getDrawableForKey(rp.process));