Android App series: Double-click the Back button to exit the program

Source: Internet
Author: User

Objective

There is a very old application skills, has been all kinds of large and small app used to be bored, that is, double-click the return key to exit the program. Writing its implementation code today is very simple and practical.

Body

Double-click the return key to exit the program, there are generally two implementation ideas, one is to use a Boolean variable to record the key event, and through the thread delay to achieve the effect; the other is directly through the record key time to calculate the timing of the implementation of the function, now share with you the Code bar, O (∩_∩) o haha ~

1, using thread delay to achieve

Private Static Booleanmbackkeypressed =false;//record If there is a first keystroke@Override Public voidonbackpressed () {if(!mbackkeypressed) {Toast.maketext ( This, "Press again to exit the program", Toast.length_short). Show (); Mbackkeypressed=true; NewTimer (). Schedule (NewTimerTask () {//delay two seconds, if out of the wrong first keystroke record        @Override Public voidrun () {mbackkeypressed=false; }},2000); }Else{//Exit Program       This. Finish (); System.exit (0); }}

Mbackkeypressed to record whether there is a record for the first press of the return key, if there is no first keystroke record, the toast prompt, and records the first keystroke record, and the startup thread erases the keystroke record after 2 seconds. If the online Chenghaine erase mbackkeypressed and press the back key, then the statement inside the else is executed to exit the program.

2, by calculating the time difference to achieve

Private LongMpressedtime = 0; @Override Public voidonbackpressed () {LongMnowtime = System.currenttimemillis ();//get the first keystroke time    if((Mnowtime-mpressedtime) > 2000) {//compare two key time differenceToast.maketext ( This, "Press again to exit the program", Toast.length_short). Show (); Mpressedtime=Mnowtime; }Else{//Exit Program       This. Finish (); System.exit (0); }}

This is accomplished by controlling the time difference, and when the return key is first pressed, the current period is assigned to Mpressedtime and the user is prompted by toast, and when the return key is pressed the second time, if the The Mpressedtime record has a time difference greater than 2 seconds to refresh the mpressedtime, and if less than 2 seconds the Else statement exits the program.

PS: It is recommended to use the time difference method to achieve this function, because the simple and stable code is much better than the thread delay.

If there is a better way to achieve, you are welcome to discuss the HA ~

Enjoy wind chimes
Source: http://www.cnblogs.com/net168/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original connection, or next time not to you reproduced.

Android App series: Double-click the Back button to exit the program

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.