Android in some apps pop-up dialog when exiting the client, there are two clicks, the first is to prompt the user whether to quit the client, the second click is the real exit app, which is two times the click Back key time interval to judge,
Realize this simple function today,
Analysis: The program to save the user's first click on the time, in Java we know, save a variable is definitely to define a member variable to save the first click of the time variable, when the second click to get the current click Time, and the first click on the time to subtract whether more than how many seconds to let the app exit, code implementation
Public long pretime; public static final Long Two_second = 2 * 1000; @Override public void onbackpressed () { Long currenttime = System.currenttimemillis (); If the time interval is greater than 2 seconds, do not process if ((currenttime-pretime) > Two_second) { //Display message Toast.maketext (this, "Press again to exit the client", 0 ). Show (); Update time pretime = currenttime; return; Intercept the event and no longer process }else{ finish ();} }
Android two times press the back button to exit the client