Android Press two times to exit the app

Source: Internet
Author: User

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:
  1. /*---onkeydown method: Press the return key two times to exit--*/
  2. Public boolean onKeyDown (int keycode, keyevent event) {
  3. long period;
  4. if (KeyCode = = event. Keycode_back) //OK press the back key
  5. {
  6. Period = System.currenttimemillis ()-mexittime; //Two time intervals pressed
  7. if    (period>) //3s two times within the press exit valid
  8. {
  9. Toast.maketext (This,"press again to exit the program", Toast.length_short). Show ();
  10. Mexittime = System.currenttimemillis ();
  11. }
  12. Else
  13. {
  14. Finish (); //End activity (master), that is, exit the app
  15. }
  16. return true;
  17. }
  18. return Super. OnKeyDown (KeyCode, event);
  19. }
  20. }
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

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.