"Go" Android implementation click two times back key to exit

Source: Internet
Author: User

In the Android application is we often have to judge the user to the return key operation, generally to prevent misoperation is the user repeatedly press two times the return button when the user is prompted to quit the application.

The basic principle of the first implementation is that when the back key is pressed, it is captured by onkeydown and the back key is determined, then the exit method is executed.
In the Exit method, the value of Isexit is first judged, if False, it is set to true, and a prompt pops up, and a message is issued after 2000 milliseconds (2 seconds), and the value is reverted to false in handler. If the back key is pressed again within 2 seconds of the sending message interval, the exit method is executed again, and the exit method is executed when the value of Isexit is true.

1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
Package com.Gaolei.Exitdemo;ImportAndroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.KeyEvent;ImportAndroid.widget.Toast;PublicClassMainactivityExtends Activity{Define a variable to identify whether to exitPrivateStaticBoolean isexit=False; Handler Mhandler=New Handler(){@OverridePublicvoidHandlemessage(Message msg){Super.Handlemessage(msg); Isexit=False;}};@OverrideProtectedvoidOnCreate(Bundle savedinstancestate){Super.OnCreate(savedinstancestate); Setcontentview(R.Layout.Activity_main);}@OverridePublicBooleanOnKeyDown(int KeyCode, KeyEvent Event){If(KeyCode= = KeyEvent.Keycode_back){Exit();ReturnFalse;}ReturnSuper.OnKeyDown(KeyCode, event);}PrivatevoidExit(){If(!isexit){Isexit=True; Toast.Maketext(Getapplicationcontext(),"Press one more time to exit the program", Toast.Length_short).show (); . sendemptymessagedelayed 02000} else {finish(); System. Exit (0} }        

The second implementation method, by recording the key time to calculate the difference in timing:

1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738
Package com.Gaolei.Exitdemo;ImportAndroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.KeyEvent;ImportAndroid.widget.Toast;PublicClassMainactivityExtends Activity{PrivateLong Exittime=0;@OverrideProtectedvoidOnCreate(Bundle savedinstancestate){Super.OnCreate(savedinstancestate); Setcontentview(R.Layout.Activity_main);}@OverridePublicBooleanOnKeyDown(int KeyCode, KeyEvent Event){If(KeyCode= = KeyEvent.Keycode_back){Exit();ReturnFalse;}ReturnSuper.OnKeyDown(KeyCode, event);}PublicvoidExit(){If(System.Currenttimemillis()-Exittime)>2000){Toast.Maketext(Getapplicationcontext(), Toast.< Span style= "color: #7d9029;" >length_short). show () exittime = System. Currenttimemillis ();  else  (); System. Exit (0} }        

Disclaimer: EoE article copyright belongs to the author, protected by law, when reproduced please be sure to include the following information in the form of hyperlinks

Original GAOLEI_XJ

Original address: http://my.eoe.cn/leigo/archive/2146.html

"Go" Android implementation click two times back key 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.