Overriding the OnKeyDown () method
System.currenttimemillis (): The function of this method is to return the current computer time, the time of the expression format is the current computer time and GMT time (GMT) January 1, 1970 0:0 0 seconds difference in the number of milliseconds
Private long exittime = 0;
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back
&& event.getaction () = = Keyevent.action_down) {
if ((System.currenttimemillis ()-Exittime) > 2000) {
Toast.maketext (Getapplicationcontext (), "Press again to exit the program",
Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else {
Finish ();
System.exit (0);
}
return true;
}
Return Super.onkeydown (KeyCode, event);
}
Overriding the Onbackpressed () method
@Override
public void onbackpressed () {
Select Back key in 2s to be valid
if (System.currenttimemillis ()-mexittime > 2000) {
Toast.maketext (This, "Press again to exit the oil friend", Toast.length_short). Show ();
Mexittime = System.currenttimemillis ();
} else {
Finish ();
All non-0 indicates an abnormal exit! 0 indicates normal exit!
System.exit (0);
}
}
Android implementation Press two times back key to exit the app