16

First of all I know it's bad to use a task manager/killer in Android and all that, but what I was wondering is how do task managers like Advanced Task Killer kill other applications?
I wanted to develop a simple application that would do this, just for the learning experience.
I tried executing the Linux command kill pid from my application but it didn't work, maybe it requires root?

So, how do I accomplish this from my application? I have a simple ListActivity that shows the currently running tasks and when a user long-presses an item I want to kill that task.

barq
  • 3,681
  • 4
  • 26
  • 39
Bhaskar Kandiyal
  • 978
  • 1
  • 10
  • 20
  • have a look at http://stackoverflow.com/questions/4921244/android-task-kill, which covers this. – Mark Allison Jun 10 '11 at 08:11
  • 1
    Thanks, but I've already read that post :) It doesn't answer my question though, the Process.killProcess function only allows one to kill my own process and not others, if I'm not wrong. I also tried it on my application but it didn't work :/ – Bhaskar Kandiyal Jun 10 '11 at 08:40

4 Answers4

12

You can send the signal using:

Process.sendSignal(pid, Process.SIGNAL_KILL);

To completely kill the process, it's recommended to call:

ActivityManager.killBackgroundProcesses(PackageName)

before sending the signal.

Bhaskar Kandiyal
  • 978
  • 1
  • 10
  • 20
  • where is ActivityManager.killBackgroundProcesses... it says that the method is undefined... – Taranasus Sep 12 '11 at 14:27
  • 1
    so does this really kill external applications? I'm not quit sure how to check this. ActivityManager.killBackgroundProcesses(PackageName); Process.sendSignal(pid, Process.SIGNAL_KILL); –  Nov 10 '11 at 09:58
  • does it? I have the same problem – Rohit Tigga Aug 21 '14 at 22:21
1

slayton has good answer in this question.I add this detail to his answer:
- when you use ActivityManager.killBackgroundProcesses(PackageName) , you can not kill foreground process.

I saw these open sources project link in K_Anas'answer to this question:
- github repository
- code.google

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

try this,

android.os.Process.killProcess(pid)

that will work...

Amit
  • 13,134
  • 17
  • 77
  • 148
-1

1- Add to manifest

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

2 - In your code

Runtime.getRuntime().exec("adb shell killall com.example.app");

Note : Your app needs to have access to adb shell system/app (root permission)

bastami82
  • 5,955
  • 7
  • 33
  • 44