25

I don't want to get on the discussion of whether a process can be killed by the user, whether it should be done that way or not.

I just want to know how almost every Android player's phone got a 'advanced task killer' which kills a process or how is it that the force close (in settings) option works just fine. I have tried many ways to kill a process, but all without result. But when I see these 'advanced task killer' I can't figure out what's wrong on my side.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AbhishekB
  • 2,111
  • 3
  • 18
  • 23
  • 1
    See: http://stackoverflow.com/questions/6303615/how-do-task-managers-kill-apps – Alex Florescu Mar 05 '12 at 08:29
  • @anothem : I have seen that post and found that Process.sendSignal(pid, Process.SIGNAL_KILL); gives an error that it cannot be resolved. – AbhishekB Mar 05 '12 at 08:38
  • Can you show the exact error and stack trace? – Alex Florescu Mar 05 '12 at 09:26
  • Process.SIGNAL_KILL cannot be resolved, its an Compile time error (eclipse),am using API level 8 – AbhishekB Mar 05 '12 at 09:41
  • 1
    It's all in android.os since API Level 1. See: http://developer.android.com/reference/android/os/Process.html maybe, try writing it as: android.os.Process.SIGNAL_KILL – Alex Florescu Mar 05 '12 at 11:51
  • yes tried it already.no errors.but still does not work.any other solution? – AbhishekB Mar 05 '12 at 12:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8534/discussion-between-abhishekb-and-anothem) – AbhishekB Mar 05 '12 at 12:36
  • Do you have this in your manifest? – Alex Florescu Mar 10 '12 at 14:11
  • You don't get to say that you don't care to get into "the discussion of whether a process can be killed by the user" and then turn around and complain when what you try doesn't work. **Fundamentally, this is not something you are supposed to be trying to do** so if you are serious about it, you are going to have to open yourself to discussion about the limitations. – Chris Stratton Dec 06 '13 at 15:46
  • http://stackoverflow.com/a/40266669/5235263 – bastami82 Oct 26 '16 at 15:46

3 Answers3

21

You can use the killBackgroundProcesses() method of ActivityManager:

    ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
    am.killBackgroundProcesses(packageName);

Please, note that your app needs to own the KILL_BACKGROUND_PROCESSES permission. Thus, in the AndroidManifest.xml, you need to include:

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37
4

Try android.os.Process:

void killMyProcess() {
    android.os.Process.killProcess(android.os.Process.myPid());
}

As it implies from the method name, you can only kill your own process by using this approach.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
-1

Suppose you have launched activities in the order A -> B -> C -> D with A as your main and launcher activity.

You can not even kill your application's process by calling Process.KillProcess(int pid) method in your D activity. You need to kill each activity D - C - B - A (in reverse order) to kill the process.

So killing other application processes is out of the question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AndroidDev
  • 2,627
  • 6
  • 29
  • 41
  • actually vishal its not even like that u must have tried finish().that does not kill the process.U can check that in the settings->Running tab.Ur application still remain in the background – AbhishekB Mar 05 '12 at 10:24
  • "So killing other application processes is out of question" thats my point.how does ' advanced task killer ' work then? – AbhishekB Mar 05 '12 at 10:25
  • 1
    This is simply untrue. **You can kill any process you own**, however Android may be surprised by this and as a result perhaps decide that it should start a replacement for the killed process. – Chris Stratton Dec 06 '13 at 15:48