I have been working on an Andorid application over the past few days. I want to implement the task manager effect and close other programs. I have read a lot of information online, many of them use the restartPackage or killBackgroundProcesses function, but this function does not really close the application. my mobile phone is the root defy and tries other operations.
① Kill-9 pid
This command is very familiar with linux and forces a process to be killed. obtain the pid of the process to be killed, and then directly execute the preceding command. After the test, the application is still running in the setting-app on the mobile phone, and the force close button can still be clicked, it does not achieve the effect.
② Use the built-in android command (am force-stop)
This was found when I checked the help of adb. The original adb operations are quite rich, not only can I start the application, send broadcast, view logs, but also can I operate on the application, for example, force close is very powerful. based on this, other applications are successfully disabled. (adb official document)
Package cn. androiddevelop. flushapk. util; import java. io. IOException; import java. io. outputStream;/*** run the android command ** @ author Yuedong Li **/public class SuUtil {private static Process process Process;/*** to terminate the process, execute the operation to call */public static void kill (String packageName) {initProcess (); killProcess (packageName); close ();} /*** initialization process */private static void initProcess () {if (process = null) try {process = Runtime. ge Truntime(cmd.exe c ("su");} catch (IOException e) {e. printStackTrace () ;}/ *** end process */private static void killProcess (String packageName) {OutputStream out = process. getOutputStream (); String cmd = "am force-stop" + packageName + "\ n"; try {out. write (cmd. getBytes (); out. flush ();} catch (IOException e) {e. printStackTrace () ;}}/*** close output stream */private static void close () {if (process! = Null) try {process. getOutputStream (). close (); process = null;} catch (IOException e) {e. printStackTrace ();}}}
Reprinted please indicate the source.