Android two times prompts to exit the application, and android two times to exit
Reprinted please indicate: http://blog.csdn.net/u012637501 Android two prompts to exit the application, the core idea is: in the main Activity re-write onKeyDown method, which is a callback method, that is, when the user presses a button on the keyboard, we can determine which button is pressed Based on the keyboard code. Then, determine the time interval (for example, 3 s) between the two buttons to determine whether to exit the application by pressing the return key twice. The specific implementation code is as follows:
- /* --- OnKeyDown method: press the two return keys to exit --*/
- Public BooleanOnKeyDown (IntKeyCode, KeyEvent event ){
- LongPeriod;
- If(KeyCode = event. KEYCODE_BACK) // press the return key.
- {
- Period = System. currentTimeMillis ()-mExitTime; // interval between two presses
- If(Period> 3000) // two presses within 3 s to exit.
- {
- Toast. makeText (This, "Exit the program again", Toast. LENGTH_SHORT). show ();
- Mexico time = System. currentTimeMillis ();
- }
- Else
- {
- Finish (); // end Activity (main), that is, exit the application
- }
- Return True;
- }
- Return Super. OnKeyDown (keyCode, event );
- }
- }
Running result: two times of pressing the return key within 3 seconds, exit the program. Otherwise, the first time you press the return key is invalid, and you are prompted to re-press again to exit the program.