Android two times prompts to exit the application, and android two times to exit

Source: Internet
Author: User

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:

  1. /* --- OnKeyDown method: press the two return keys to exit --*/
  2.  Public BooleanOnKeyDown (IntKeyCode, KeyEvent event ){
  3. LongPeriod;
  4. If(KeyCode = event. KEYCODE_BACK) // press the return key.
  5. {
  6. Period = System. currentTimeMillis ()-mExitTime; // interval between two presses
  7. If(Period> 3000) // two presses within 3 s to exit.
  8. {
  9. Toast. makeText (This, "Exit the program again", Toast. LENGTH_SHORT). show ();
  10. Mexico time = System. currentTimeMillis ();
  11. }
  12. Else
  13. {
  14. Finish (); // end Activity (main), that is, exit the application
  15. }
  16. Return True;
  17. }
  18. Return Super. OnKeyDown (keyCode, event );
  19. }
  20. }
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.