5

I'm executing the following piece of code:

ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> procInfos = actvityManager.getRunningTasks(1000);

Then, I kill one process I'm interested in with

actvityManager.killBackgroundProcesses(process.baseActivity.getPackageName());

where process is an entry from procInfos.

The problem is if I run getRunningTasks again - it would still show the process I (presumably) killed, while a task manager for Android listed that process before calling killBackgroundProcesses and removed it from list after that call.

So, any ideas on how does task manager get its list of running processes? And is it normal that I have successfully killed 3rd-party process on an unrooted device?

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • 1
    This would have been better as a new question, since it's quite different from what you originally asked. In any case, you can find most of the answer to your current question here: http://stackoverflow.com/questions/8814696/how-to-kill-currently-running-task-in-android , in particular the second answer. – Rob Pridham Mar 23 '12 at 14:47
  • @RobPridham: thank you very much, I didn't see that question. And my rephrased question doesn't seem too different from the original to me... – Violet Giraffe Mar 23 '12 at 14:50

1 Answers1

5

'Running' doesn't mean that a user started it; it might be listening for events, doing a scheduled background sync or anything else.

ActivityManager.getRunningTasks() might be closer to what you want, but in essence you are always going to have this problem, because the user is not in full control over what is currently active.

Rob Pridham
  • 4,780
  • 1
  • 26
  • 38
  • Makes sense, but I still have no idea as to why Gallery shows up as "running" because I rebooted the device and surely didn't start Gallery at all. – Violet Giraffe Mar 23 '12 at 13:32
  • Could be any number of reasons - as just one example, an application can subscribe to the system boot event, and receiving it will cause it to be started, whereupon it will count as running until the system decides to kill it. – Rob Pridham Mar 23 '12 at 13:41
  • Thanks a lot. `getRunningTasks()` really seems to do what I need. One last question: do you, by chance, know how to get a corresponding process name for a task (`ActivityManager.RunningTaskInfo`) returned by `getRunningTasks`? – Violet Giraffe Mar 23 '12 at 13:52
  • 1
    Yes - the baseActivity property of that object will lead you to a package name, which you can match against the values in the pkgList property of the processes found in your original post. – Rob Pridham Mar 23 '12 at 14:04
  • Note that as of Android 5.0 Lollipop (API Level 21), ActivityManager.getRunningTasks() has been deprecated. http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int) – jdev Nov 28 '15 at 23:05