in the development of the application, there is a feature is very common, that is, quickly double-click the Return button, and then realize the function of exiting activity. I read a lot of information on the Internet code, summed up, there are two main ways to better. One is the time delay execution of the thread, and the other is the realization of recording the key times. Now share with you, the code is as follows, I hope to help you:
The first: the use of thread-delay implementation:
private int mbackkeypressedtimes = 0;
@Override public void onbackpressed () {if (Mbackkeypressedtimes = = 0) {Toas T.maketext (This, "Press once again to exit the program ", Toast.length_short). Show (); mbackkeypressedtimes = 1; & nbsp new Thread () { & nbsp; @Override public void Run ( ) { Try { &nbs p; thread.sleep (&NB);Sp & nbsp; } catch (Interruptedexception e) {  &NB Sp & nbsp; e.printstacktrace (); & nbsp } finally { & nbsp mbackkeypressedtimes = 0; & nbsp; & nbsp;   } & nbsp; } and nbsp; }.start (); return; else{ & nbsp; This.activity.finish (); } } super.onbackpressed (); }
Second: The use of the Computation time difference implementation (personally think this way is relatively simple, and not prone to anomalies, the code is more secure)
Private long exittime = 0;
public void Exitapp ()
{
if ((System.currenttimemillis ()-Exittime) > 2000)
{
Toast.maketext (This.activity, "Press again to exit the program", Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else
{
This.activity.finish ();
}
Third
Private LongLastpressedtime;Private Static Final IntPERIOD= 2000;@OverridePublic BooleanOnKeyDown(IntKeyCode, KeyEventEvent) { If (Event.getKeyCode() == KeyEvent.Keycode_back) { Switch (Event.Getaction()) { Case KeyEvent.Action_down: If (Event.Getdowntime() -Lastpressedtime<PERIOD) {Finish(); } Else { Toast.Maketext(Getapplicationcontext(), "press again to exit." , toast.show (); Lastpressedtime = Event.} return true } } return false; /span>
Android Double click Back key to Exit program implementation