Android completely exits the program code again, and the android program code
If you need to reprint please indicate the source: http://blog.csdn.net/itas109
QQ technology exchange group: 129518033
Preface:
The previously used exit program uses finish (), which can only exit the current Activity. If there are more than one Activity, you cannot exit at a time.
1. Exit the application tool class.
/***** @ Author itas109 * TODO is used to exit the Application completely */public class ExitAPPUtils extends Application {private List <Activity> activityList = new shortlist <Activity> (); private static ExitAPPUtils instance; private ExitAPPUtils () {}// obtain the unique ExitAPPUtils instance public static ExitAPPUtils getInstance () {if (null = instance) in singleton Mode) {instance = new ExitAPPUtils ();} return instance;} // Add the Activity to the container public void addActivity (Activity activity) {activityList. add (activity);} // traverse all activities and finish public void exit () {for (Activity: activityList) {activity. finish ();} System. exit (0 );}}
2. Usage
Add the following code to the onCreate method of each Activity:
ExitAPPUtils.getInstance().addActivity(this);
3. implement full exit once again
Define time
private long exitTime = 0;
Exit code and put it in the Activity to be exited.
public boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){ if((System.currentTimeMillis()-exitTime) > 2000){ ToastUtils.show(getApplicationContext(), R.string.exit_hint);exitTime = System.currentTimeMillis(); } else {ExitAPPUtils.getInstance().exit();//finish();//System.exit(0);}return true; }return super.onKeyDown(keyCode, event);}}
4,
If you need to reprint please indicate the source: http://blog.csdn.net/itas109
QQ technology exchange group: 129518033