It is necessary to give a hint before the user exits the application, because it is possible that the user does not really want to quit, but just accidentally pressed the return key, most of the practice is to give a dialog before the application exits, I think this is not very friendly, users have to move their fingers to press the dialog button. Personally feel "Press again return key exit program" is best practice, the implementation is also very simple, directly on the code:
1 Private LongExittime = 0;2 3 @Override4 Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {5 if(KeyCode = = Keyevent.keycode_back && event.getaction () = =Keyevent.action_down) { 6 if((System.currenttimemillis ()-exittime) > 2000){ 7Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show (); 8Exittime =System.currenttimemillis (); 9}Else {Ten finish (); OneSystem.exit (0); A } - return true; - } the return Super. OnKeyDown (KeyCode, event); -}
Reference Original: http://www.cnblogs.com/jxgxy/archive/2012/08/23/2653404.html
"Android Notes" Android implementation "Click two times Return key exit"