Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill another process/application programmatically [duplicate]

Tags:

android

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.

like image 464
AbhishekB Avatar asked Mar 05 '12 08:03

AbhishekB


2 Answers

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" />
like image 50
Paolo Rovelli Avatar answered Nov 06 '22 19:11

Paolo Rovelli


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.

like image 30
Ilya Gazman Avatar answered Nov 06 '22 20:11

Ilya Gazman