Search code examples
javaandroidandroid-listviewandroid-package-managersactivity-manager

Got error while trying to get running app icon from ActivityManager.RunningTaskInfo


i'm trying to making a app that shows running app icon in custom listview, and switch to corresponding app when click icon. i think i should use "ActivityManager.RunningTaskinfo", "PackageManager" and "intent" to make it so i'm trying.. but i got error on my code.. i got error on "topActivity" How can i fix this error? and how to show "rtid" which is the icon that i got from activitymanager and packagemanager in the custom listview using such as "Drawable[] images = new Drawable[packs.size()];"

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(100);
ApplicationInfo appInfo = getPackageManager().getApplicationInfo(tasks.topActivity.getPackageName(), 0);
Drawable rtid = getPackageManager().getApplicationIcon(appInfo);

Solution

  • I think you can simply this a bit.

    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(100);
    List <Drawable> applicationDrawables = new ArrayList <Drawable>();
    PackageManager pacMgr = getPackageManager();
    
      for (ActivityManager.RunningTaskInfo runningTask: tasks)
      {
        try {
          applicationDrawables.add (pacMgr.getApplicationIcon(runningTask.topActivity.getPackageName()));
        } catch (NameNotFoundException e) {
          e.printStackTrace();
        }
      }
    

    Your main problem was that you were trying to get topActivity for a List rather than just one RunningTaskInfo package.

    As for displaying the Drawables, you will probably have to create your own custom adapter, which isn't very hard.