android-Double-click Back to exit the app

Source: Internet
Author: User

1 @Override2      Public BooleanOnKeyDown (intKeyCode, keyevent event)3     {4         if(KeyCode = =keyevent.keycode_back) {5 Quitapp ();6             return false;7}Else{8             return Super. OnKeyDown (KeyCode, event);9         }Ten     } One     /** A * Quit App -      */ -     Private voidQuitapp () { the         if(isexit) { -Intent Intent =NewIntent (intent.action_main); - intent.addcategory (intent.category_home); - startactivity (intent); +System.exit (0); -}Else{ +Isexit =true; ATools.showtext (Mcontext, "Press again to exit the app"); atMhandler.sendemptymessagedelayed (0, 2000); -         } -     } -Handler Mhandler =NewHandler () { -          Public voidhandlemessage (android.os.Message msg) { -             Super. Handlemessage (msg); inIsexit =false; -         }; to};

The second type:

1 @Override2      Public BooleanOnKeyDown (intKeyCode, keyevent event)3     {4         if(KeyCode = = Keyevent.keycode_back && event.getrepeatcount () = = 0)5 Quitapp ();6         return false;7     }8     Private LongExittime = 0;9     Private voidQuitapp () {Ten         if(System.currenttimemillis ()-Exittime > 2000){ OneTools.showtext (Mcontext, "Press again to exit the app"); AExittime =System.currenttimemillis (); -}Else{ - finish (); the         } -}

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.