Android, although there are many methods (API or shell command) to kill the background ' service ', but there are still many programs in a few seconds to start again, resulting in no real killing. Here the main focus is on how to kill Android backend services like 3,601, without booting again.
How to kill a background application
* Android.os.Process.killProcess (PID);
* Activitymanager.killbackgroundprocesses (Pkgname);
* kill-9 PID
These three methods can "kill" the background application, but will be self-booting, for the previous two direct use of the Android API is OK, so it is not more introduced. Here is a little introduction to the last method of usage: kill-9 pid;
Method: Kill-9 pid This is actually a shell command, we know that Android is the bottom layer of the Linux system, so on Android can use all the Linux terminal commands. So how to combine in code, stick a piece of code
private void KillProcess (String pid) {
Process sh = null;
DataOutputStream OS = null;
try {
SH = runtime.getruntime (). EXEC ("su");
OS = new DataOutputStream (Sh.getoutputstream ());
Final String Command = "kill-9" + pid + "\ n";
Os.writebytes (Command);
Os.flush ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Sh.waitfor ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
The most important function of this method is to tell you how to execute the Linux shell command in an Android program.
Kill background Service without starting: The AM Activity Manager Command believes that many people are familiar with the AM command, which is a command under the/system/bin/directory in the Android system. Not only can start an application under the terminal, but also can start service, send broadcast and intent action, force stop process and so on, very powerful. Here we need to use a function is to force stop the application!
For the introduction and use of the command AM, the official website to the very situation, please refer to the Android website: http://developer.android.com/tools/help/adb.html#am
The methods and functions we use are: AM force-stop <PACKAGE>
Here is an example of our code
private void forcestopapk (String pkgname) {
Process sh = null;
DataOutputStream OS = null;
try {
SH = runtime.getruntime (). EXEC ("su");
OS = new DataOutputStream (Sh.getoutputstream ());
Final String Command = "am force-stop" +pkgname+ "\ n";
Os.writebytes (Command);
Os.flush ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Sh.waitfor ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Through this code above, we call the forcestopapk method, passing an application's package name, then we can kill the corresponding Android program, not automatically start.
Force end Android Process