Android two times press the return key to exit the application

Source: Internet
Author: User

I didn't refer to other people's methods on the Internet before, and my ideas were always dull. The method used previously is to set an int sign, 0 and 1. Determine if it is 0, no, add 1, yes, and exit. This low-level error is really embarrassing. As a result, it will exit after several long intervals. Later I came up with a timer to set the time. Low efficiency. In fact, java has an API that can directly obtain the current time, and the time minus the previous time will not be able to get the interval?

Code:

Private long mExitTime; // Exit Time

// Capture the key @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0) {if (System. currentTimeMillis ()-mExitTime)> 2000) {Toast. makeText (this, "once again withdraw the vertical and horizontal material price", Toast. LENGTH_SHORT ). show (); mExitTime = System. currentTimeMillis ();} else {SysApplication. getInstance (). exit ();} return true;} return super. onKeyDown (keyCode, event );}

Here SysAppication. getInstance (). exit (); is a class encapsulated by myself. Replace your own.

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.