Android Notes-two ways to double-exit programs

Source: Internet
Author: User

First, you get the back key by OnKeyDown. If this is the next step.

One of the two methods is to determine whether to exit by determining the time between successive clicks of the back key, and the other is to set a status value to determine whether to exit by changing the status value.

1.

1     Private  LongExittime=0;//determine whether to exit based on time between consecutive clicks2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8     }9 Ten @Override One      PublicBoolean OnKeyDown (intKeyCode, KeyEventEvent) { A  -         if(keycode==keyevent.keycode_back) { - exit (); the             return false; -         } -         returnSuper.onkeydown (KeyCode,Event); -     } +     /* - Judging by the time of the Click +     */ A  at     Private voidexit () { -         if(System.currenttimemillis ()-exittime> -){ -Toast.maketext (Getapplicationcontext (),"One more click to exit", Toast.length_short). Show (); -Exittime=System.currenttimemillis (); -}Else { - finish (); inSystem.exit (0); -         } to     }

2. Change the status value by sending a message with handler delay.

    Private StaticBoolean isexit=false;//determine whether to exit based on identityHandler Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {super.handlemessage (msg); Isexit=false;    }    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override PublicBoolean OnKeyDown (intKeyCode, KeyEventEvent) {        if(keycode==keyevent.keycode_back)            {exit (); return false; }        returnSuper.onkeydown (KeyCode,Event); }    /** 1. Define whether an identity is highlighted **/    Private voidexit () {if(isexit) {isexit=true; Toast.maketext (Getapplicationcontext (),"One more click to exit", Toast.length_short). Show (); //send exit status with handler delayHandler.sendemptymessagedelayed (0, -); }Else{finish (); System.exit (0); }    }

Android Notes-two ways to double-exit programs

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.