Android Press once again to exit

Source: Internet
Author: User

There are two ways of doing this:

1). First click on the flag State, prompt and then press the exit, and then start the time, if more than 3 seconds did not press back again, all the state to zero, conversely, if the return key in 3 seconds again to exit.

BooleanIsexit =false; intTime = 0; @Override Public BooleanBack () {//TODO auto-generated Method Stub        if(!isexit) {Isexit=true; FinalTimer timer =NewTimer (); Timer.schedule (NewTimerTask () {@Override Public voidrun () {//TODO auto-generated Method Stubtime++; if(Time = = 3) {Isexit=false; time=0;                        Timer.cancel (); } System.out.println ( time+""); }            }, 0,1000); ShowMsg ("Press one more time to exit"); } Else if(Isexit && Time < 3) {finish (); }        return true; } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        //TODO auto-generated Method Stub        if(keycode==keyevent.keycode_back) {            returnBack (); }        return Super. OnKeyDown (KeyCode, event); }

2). First click on the time to write down the current time, the second click to take the current time and the last record time to do poor, if less than 3 seconds to exit, and vice versa will assign this time to mark time, prompt and then press the exit.

LongLasttime; @Override Public BooleanBack () {LongCurrtime=System.currenttimemillis (); if(currtime-lasttime>3000) {showmsg ("Press one more time to exit"); Lasttime=Currtime; }Else{finish (); }        return false; } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        //TODO auto-generated Method Stub        if(keycode==keyevent.keycode_back) {            returnBack (); }        return Super. OnKeyDown (KeyCode, event); }

Android Press once again to exit

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.