Three methods for completely exiting the Android program, android three methods

Source: Internet
Author: User

Three methods for completely exiting the Android program, android three methods

 

1. Local Dalvik VM Method

Android. OS. process. killProcess (android. OS. process. myPid () // obtain the PID. Currently, only this API is available for obtaining the PID. Otherwise, enumerate other processes from/proc, you may not have the permission to end other processes.
System. exit (0); // standard exit method of general java and c #. If the returned value is 0, the System Exits normally.

2. Task Manager Method

First, it must be noted that this method runs at the Android 1.5 API Level 3 or above, and the permission android. permission. RESTART_PACKAGES: You can directly end your package by using the restartPackage method of the ActivityManager class. The parameter is package name, which is passed through getSystemService (Context. ACTIVITY_SERVICE) to instantiate the ActivityManager object. This method is provided by the system, but the declared permissions need to be displayed.

3. According to the Activity declaration cycle

We know that the window class of Android provides A historical stack, and we can implement it skillfully through the stack principle. Here we add the Intent icon directly to window A when window A opens window B. FLAG_ACTIVITY_CLEAR_TOP. When B is enabled, all activities in the process space are cleared.

Use the following code to call window B in window:

Intent intent = new Intent ();
Intent. setClass (Android123.this, CWJ. class );
Intent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); // pay attention to the FLAG settings of the row.
StartActivity (intent );

Next, use the finish method to exit all the services in window B.

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