Disable other programs in Android

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.