Android implementation Press two times back key to exit the app interface

Source: Internet
Author: User

By pressing the back key two times, the Toast prompt pops up and the Exit function is pressed again. The key code is as follows:

The first way:

 /** * Press two times to return the key to exit */private int keybackclickcount = 0;        @Override protected void Onresume () {super.onresume ();    Keybackclickcount = 0;        } @Override public boolean onKeyDown (int keycode, keyevent event) {//TODO auto-generated method stub                    if (keycode = = keyevent.keycode_back) {switch (keybackclickcount++) {case 0:                            Toast.maketext (this, getresources (). getString (R.string.press_again_exit),                    Toast.length_short). Show ();                    Timer timer = new timer ();                            Timer.schedule (New TimerTask () {@Override public void run () {                        Keybackclickcount = 0;                    }}, 3000);                Break                    Case 1:mframetv.setvisibility (view.visible); Mimgtv.setvisibility (View.visible);                    Animation anim = animationutils.loadanimation (Mainactivity.this, R.anim.tv_off);                    Anim.setanimationlistener (New Tvoffanimlistener ());                    Mimgtv.startanimation (ANIM);                Break            Default:break; } <span style= "color: #ff0000;"                 > Return true;</span>} else if (keycode = = Keyevent.keycode_menu) {if (sm.ismenushowing ()) {            Toggle ();            } else {showmenu ();    }} return Super.onkeydown (KeyCode, event); }

The second way:

/** * Exit */private Boolean iswaitingexit = False by pressing the return key two times consecutively, @Overridepublic boolean onKeyDown (int keycode, keyevent event) { TODO auto-generated method stubif (keycode = = Keyevent.keycode_back) {if (iswaitingexit) {iswaitingexit = False;mframet V.setvisibility (0); mimgtv.setvisibility (0); Animation anim = animationutils.loadanimation (Mainactivity.this, R.anim.tv_off); Anim.setanimationlistener (new Tvoffanimlistener ()); Mimgtv.startanimation (anim);} else {Toast.maketext (This,getresources (). getString (R.string.press_again_exit), Toast.length_short). Show (); Iswaitingexit = true; Timer timer = new timer (); Timer.schedule (new TimerTask () {@Overridepublic void run () {iswaitingexit = false;}}, +); Retu RN true;} return true;} else if (keycode = = Keyevent.keycode_menu) {if (sm.ismenushowing ()) {toggle ();} else {showmenu ();}} Return Super.onkeydown (KeyCode, event);}


Android implementation Press two times back key to exit the app interface

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.