Reprint please indicate: http://blog.csdn.net/u012637501 Android to exit the application by two prompts, the core idea is: in the main activity to override the OnKeyDown method, which is a callback method, that is, when the user presses a button on the keyboard, We can determine which key is pressed according to the keyboard code. Then, the calculation is calculated two times the time interval (such as 3s) of the return key is pressed to achieve two press the return key to exit the application. The specific implementation code is as follows:
- /*---onkeydown method: Press the return key two times to exit--*/
- Public boolean onKeyDown (int keycode, keyevent event) {
- long period;
- if (KeyCode = = event. Keycode_back) //OK press the back key
- {
- Period = System.currenttimemillis ()-mexittime; //Two time intervals pressed
- if (period>) //3s two times within the press exit valid
- {
- Toast.maketext (This,"press again to exit the program", Toast.length_short). Show ();
- Mexittime = System.currenttimemillis ();
- }
- Else
- {
- Finish (); //End activity (master), that is, exit the app
- }
- return true;
- }
- return Super. OnKeyDown (KeyCode, event);
- }
- }
Operation Result: two times press the return key time interval within 3s, exit the program. Otherwise, the first time you press the back key is not valid, and then re-prompt "Press again to exit the program" information.
Android Press two times to exit the app