Android app exits the program by pressing the return key twice

Source: Internet
Author: User

 

// ExitProgramPrivate Static Boolean isexit = false; // time trigger Private Static timer texit = NULL;

 

 

 

Public Boolean onkeyup (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) {If (isexit = false) {isexit = true; If (texit! = NULL) {texit. cancel (); // remove the original task from the queue} // re-instance a timer texit = new timer (); timertask task = new timertask () {@ overridepublic void run () {isexit = false ;}}; toast. maketext (this, "exit the program again", toast. length_short ). show (); // triggers the task texit in two seconds. schedule (task, 2000);} else {finish (); system. exit (0);} return true;} return Super. onkeyup (keycode, event );}

The principle is simple: Define a timertask timer and an exit program id isexit. Press the return key for the first time, assign the value isexit to true, enable the timertask timer, and assign the value isexit to false after two seconds, therefore, if you press the return key again within these two seconds, the program will exit. Otherwise, the timer timertask task is executed, which indicates false. The principle is so simple ~

 

 

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.