If you want to reprint please indicate the source: http://blog.csdn.net/itas109
QQ Technology Group: 129518033
Objective:
The exit program used earlier uses finish (), which only exits the current activity. If you have more than one activity, you can't quit at once.
1. Exit the Application Tools class
/** * * @author itas109 * TODO is used to completely exit the application */public class Exitapputils extends application { private List<ac tivity> activitylist = new linkedlist<activity> (); private static exitapputils instance; Private Exitapputils () { } //In singleton mode gets the unique Exitapputils instance public static Exitapputils getinstance () { if ( NULL = = Instance) { instance =new exitapputils (); } return instance; } Add activity to container public void addactivity (activity activity) { activitylist.add (activity); } Traverse all Activity and finish public void exit () {for (Activity activity:activitylist) { activity.finish (); c21/>} system.exit (0);} }
2. How to use
Add the following code to each activity's OnCreate method
Exitapputils.getinstance (). addactivity (this);
3. Achieve a full exit effect by pressing again
Define Time
Private long exittime = 0;
Exit the code and put it in the activity you want to exit.
public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {if ((System.currenttimemillis ()-exittime) >) {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 want to reprint please indicate the source: http://blog.csdn.net/itas109
QQ Technology Group: 129518033
Android Press once again to completely exit the program code