It is necessary to give a hint before the user exits the application. Because the user may not really want to quit, but only accidentally pressed the return key, most of the application is in the application to quit before giving a dialog, I think this is not very friendly, users have to move their fingers to press dialog button. Personally feel "once again to return the key to exit the program" is best practice, the implementation is also very simple, directly on the code:
Copy Code code as follows:
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 Exit Again", Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else {
Finish ();
System.exit (0);
}
return true;
}
Return Super.onkeydown (KeyCode, event);
}