The APP exits completely.

Source: Internet
Author: User

The APP exits completely.
1. local method of Dalvik VM android. OS. process. killProcess (android. OS. process. myPid () // obtain the PID System. exit (0); // standard exit method of conventional java and c #. If the returned value is 0, the system Exits normally in Xiaomi 3 and can only disable the current Activity, it may be available in other android systems, but at least it is not applicable.
2. we know that the window class of Android provides A historical stack. We can use the stack principle to implement it skillfully. Here we add the logo Intent directly to the Intent when opening window B in window. 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 setting startActivity (intent) for the FLAG of the row );

3. the task manager method must first indicate that the method runs at the Android 1.5 API Level 3 or above, and the permission ActivityManager am = (ActivityManager) getSystemService (Context. ACTIVITY_SERVICE); am. restartPackage (getPackageName (); the system will kill all processes and services under the package, and you will be able to kill them. Note that you can add <uses-permission android: name = \ "android. permission. RESTART_PACKAGES \ "> </uses-permission> is not recommended because of incompatibility.

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.

A List set is maintained during the lifecycle of the entire application, which can easily generate memory accumulation. Memory accumulation and overflow are poor processing. If there are many activities not closed, when you exit, there will obviously be a card process, and the user experience is very good.

The Code is as follows:

Public class ScreenManager {
Private static Stack <Activity> activityStack;
Private static ScreenManager instance;
Private ScreenManager (){
}
Public static ScreenManager getScreenManager (){
If (instance = null ){
Instance = new ScreenManager ();
}
Return instance;
}
// Exit the stack top Activity
Public void popActivity (Activity activity ){
If (activity! = Null ){
Activity. finish ();
ActivityStack. remove (activity );
Activity = null;
}
}


// Obtain the Activity at the top of the current stack
Public Activity currentActivity (){
Activity activity = activityStack. lastElement ();
Return activity;
}


// Push the current Activity into the stack
Public void pushActivity (Activity activity ){
If (activityStack = null ){
ActivityStack = new Stack <Activity> ();
}
ActivityStack. add (activity );
}
// Exit all activities in the stack
Public void popallactivityeffectone (Class cls ){
While (true ){
Activity activity = currentActivity ();
If (activity = null ){
Break;
}
If (activity. getClass (). equals (cls )){
Break;
}
PopActivity (activity );
}
}
}

5. we recommend that you disable the entire application by broadcasting the custom broadcast to completely exit the app. The idea is to create a parent class for all the activities, you only need to register a custom disable application broadcast in the parent class. It is feasible to think about this method carefully. You only need to dynamically register a broadcast in the base class to implement this function.
The Code is as follows:
ExitAppReceiver:
Package com. example. appexitdemo; import android. app. activity; import android. app. service; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. support. v4.app. fragmentActivity; // custom a broadcast receiver to receive the application exiting the broadcast. public class ExitAppReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {if (context! = Null) {if (context instanceof Activity) {(Activity) context ). finish ();} else if (context instanceof FragmentActivity) {(FragmentActivity) context ). finish ();} else if (context instanceof Service) {(Service) context ). stopSelf ();}}}}
BaseActivity:
Package com. example. appexitdemo; import android. app. activity; import android. content. intentFilter; import android. OS. bundle; // The base class public class BaseActivity extends Activity of all activities in the application {private ExitAppReceiver exitReceiver = new ExitAppReceiver (); // customize the exit Action, the actual application should be placed in the Constant class of the entire application. private static final String EXIT_APP_ACTION = "com. micen. exit_app "; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); registerExitReceiver ();} private void registerExitReceiver () {IntentFilter exitFilter = new IntentFilter (); exitFilter. addAction (EXIT_APP_ACTION); registerReceiver (exitReceiver, exitFilter);} private void Merge () {unregisterReceiver (exitReceiver);} @ Override protected void onDestroy () {super. onDestroy (); unRegisterExitReceiver () ;}@ Override protected void onStart () {super. onStart () ;}@ Override protected void onStop () {super. onStop ();}}

Add the following broadcast sending code to the exit method of the App:
Intent intent = new Intent (); intent. setAction (EXIT_APP_ACTION); sendBroadcast (intent );

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.