Steps for completely exiting the android Program

Source: Internet
Author: User

From: http://www.cnblogs.com/jh5240/archive/2011/11/09/2243734.html

Method:

The exit Method for versions 1.5 to 2.1 is

Activitymanager = (activitymanager) Context. getsystemservice (context. activity_service );
Activitymanager. restartpackage ("package name ");

Version 2.2 must clear the background data:

Step 1:
/**
* Activitylist: all activity objects, used to finish all the objects when exiting; added to the set when the activity follows oncreate
*/
Public static list <activity> activitylist = new arraylist <activity> ();

Step 2:
/**
* Page Initialization
*
* @ Param savedinstancestate
*/
Protected void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Activitylist. Add (this );

Step 3:
/**
* Exit the client.
*
* @ Param context Context
*/
Public static void exitclient (context)
{
Log. D (TAG, "----- exitclient -----");
// Close all activities
For (INT I = 0; I <activitylist. Size (); I ++)
{
If (null! = Activitylist. Get (I ))
{
Activitylist. Get (I). Finish ();
}
}
Activitymanager = (activitymanager) Context. getsystemservice (context. activity_service );
Activitymanager. restartpackage ("package path ");
System. Exit (0); // The Android program only causes the activity to finish (), but does not completely exit.
}

Add system. Exit (0) here );
Remember to add permissions
<Uses-Permission Android: Name = "android. Permission. restart_packages"/>

 

Method B:

Android. OS. process. killprocess (Android. OS. process. mypid ());

To sum up the following four methods:

 

The android program has a lot of activities, such as the main window A, and calls the sub-window B. If you finish () directly in B, the next display is. In B, how does one close the entire android application? I have summarized several simple implementation methods.

1. Local Dalvik VM Method

Android. OS. process. killprocess (Android. OS. process. mypid () // obtain the PID.

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 can run at the android 1.5 API level 3 or above and requires permissions.

Activitymanager AM = (activitymanager) getsystemservice (context. activity_service );

Am. restartpackage (getpackagename ());

The system will kill all the processes, services, and services under the package, and you will be able to kill them. Pay attention to adding permissions.

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.

 

4. customize an actiivty stack. The same principle is true. However, an activity stack in singleton mode is used to manage all activities. And provides the method to exit all activities. The Code is as follows:

Public class screenmanager {Private Static stack activitystack; Private Static screenmanager instance; private screenmanager () {} public static screenmanager getscreenmanager () {instance = new screenmanager ();} return instance ;} // exit the stack top activity public void popactivity (activity) {activity. finish (); activitystack. remove (activity); Activity = NULL ;}// obtain the activity public activity currentactivity () {activity = activitystack on the top of the current stack. lastelement (); Return activity;} // push the current activity to the public void pushactivity (activity) {activitystack = new stack ();} activitystack. add (activity) ;}// exit all activity public void popallactivityeffectone (class CLs) {While (true) {activity = currentactivity (); break ;} popactivity (activity );}}}

 

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.