Android completely closes the application

Source: Internet
Author: User

When I encountered a problem in a working program that needed to completely shut down the application, I found a lot of articles on the network, and each article uses System. exit (0) or android. OS. process. killProcess (android. OS. process. myPid (), but I have tried it, System. exit (0) does not work at all, but android. OS. process. killProcess (android. OS. process. myPid () can only close the current Activity, that is, it is effective for an application that only has a single Activity. If there are many external activities, it will be powerless.
The following describes how to completely close applications of multiple activities:
The following methods are provided in the ActivityManager class:
/**
* Have the system perform a force stop of everything associated
* The given application package. All processes that share its uid
* Will be killed, all services it has running stopped, all activities
* Removed, etc. In addition, a {@ link Intent # ACTION_PACKAGE_RESTARTED}
* Broadcast will be sent, so that any of its registered alarms can * be stopped, notifications removed, etc.
*
* You must hold the permission * {@ link android. Manifest. permission # RESTART_PACKAGES} to be able
* Call this method.
*
* @ Param packageName The name of the package to be stopped.
*/
Public void restartPackage (String packageName ){
Try {
ActivityManagerNative. getDefault (). restartPackage (packageName );
}
Catch (RemoteException e ){}
}
To close the entire application, you only need to run the following two lines of code:
ActivityManager activityMgr = (ActivityManager) this. getSystemService (ACTIVITY_SERVICE );
ActivityMgr. restartPackage (getPackageName ());
Finally, you need to add this permission:
<! -- Disable application permissions -->
<Uses-permission android: name = "android. permission. RESTART_PACKAGES"/>
 
Many users may find that their Android program has A lot of activities, such as the main window A, calling subwindow B, and how to close the entire Android Application in B? Here are three simple methods for implementation.
First, you need to describe how to use finish () in B. Next, the phone displays the main window A, so let's take A look at how it is implemented.
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 ports in window B.

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.