0

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);
sukso
  • 145
  • 1
  • 8
  • Can you post the LogCat? IIRC, you also need to have a certain permission to get the list of running tasks. Possibly ` – A--C Feb 16 '13 at 15:59
  • I cannot launch app bacause of the wrong code"topActivity" i'm asking hot to fix it and i already added that permission on my manifest – sukso Feb 17 '13 at 00:16
  • Your main problem lies with the fact that you have to get only *one* RunningTaskInfo from the `List` of running tasks. That way `topActivity` is defined. – A--C Feb 17 '13 at 00:26

1 Answers1

1

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.

A--C
  • 36,351
  • 10
  • 106
  • 92
  • Thanks you so much for helping me! I already created custom arrayadapter to make custom listview i will try this code soon! – sukso Feb 17 '13 at 00:48
  • @sukso No problem! I tried it with an `ImageButton` and it did work. Also, you have to use a `try/catch` block because of the thrown Exception. Lastly, If you have a lot of Running tasks, the next step once you get this working is to use an `AsyncTask` so you don't hold up the UI Thread. – A--C Feb 17 '13 at 01:09