Android kill process Several methods detailed introduction _android

Source: Internet
Author: User

Android kills the process:

For the end of the process in the development of app application is still necessary, here sorted out three ways, we can choose according to the needs.

When the application is no longer in use, you usually need to close the application, you can use the following three ways to turn off the Android application:

The first method: Get the ID of the current process and then kill the process.

Android.os.Process.killProcess (Android.os.Process.myPid ())

Then practice:

<relativelayout xmlns:android= ""
  xmlns:tools= "http:// "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android:paddingbottom= "@dimen/activity_vertical_margin"
  android:paddingleft= "@dimen/activity_ Horizontal_margin "
  android:paddingright=" @dimen/activity_horizontal_margin "
  android:paddingtop=" @dimen /activity_vertical_margin "
  tools:context=". Mainactivity ">

    android:layout_width=" wrap_content "
    android:layout_height=" Wrap_ Content "
    android:layout_centerhorizontal=" true "
    android:layout_centervertical=" true "
    android:o nclick= "click"
    android:text= "Suicide"/>


Add Click event Code

 public void Click {
    //get id
    int pid = Android.os.Process.myPid () for the current process;
    This method can only be used for suicide operation
    Android.os.Process.killProcess (PID);


Before clicking the button, you can see the process

After clicking the button, the process commits suicide

Second method: Terminates the currently running Java Virtual machine, causing the program to terminate

System.exit (0);


The third method is to force the shutdown of everything associated with the package execution

Activitymanager manager = (Activitymanager) getsystemservice (context.activity_service); 
Manager.restartpackage (Getpackagename ());
<uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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: 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.