Android users click the return key twice to exit

Source: Internet
Author: User

For Android applications, we often need to determine the user's operation on the Return key. Generally, to prevent misoperation, the user is prompted whether to exit the application when the user presses the return key twice in a row.

The basic principle of the first implementation is that when the BACK key is pressed, it will be captured by onKeyDown. If it is determined to be the BACK key, the exit method is executed.
In the exit method, the isExit value is determined first. If it is set to false, the isExit value is set to true. A prompt is displayed, and a message is sent after 2000 milliseconds (2 seconds, in Handler, this value is restored to false. If you press the BACK key again within 2 seconds of the message sending interval, the exit method is executed again. If the value of isExit is already true, the exit method is executed.

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
Package com. gaolei. exitdemo; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. keyEvent; import android. widget. toast; public class MainActivity extends Activity {// defines a variable to identify whether to exit private static boolean isExit = false; Handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg ); IsExit = false ;}}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {exit (); return false;} return super. onKeyDown (keyCode, event);} private void exit () {if (! IsExit) {isExit = true; Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); // use handler to send the change status message mHandler. sendEmptyMessageDelayed (0, 2000);} else {finish (); System. exit (0 );}}}

The second method is to calculate the time difference by recording the key time:

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738
Package com. gaolei. exitdemo; import android. app. activity; import android. OS. bundle; import android. view. keyEvent; import android. widget. toast; public class MainActivity extends Activity {private long exitTime = 0; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) {exit (); return false;} return super. onKeyDown (keyCode, event);} public void exit () {if (System. currentTimeMillis ()-exitTime)> 2000) {Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); exitTime = System. currentTimeMillis ();} else {finish (); System. exit (0 );}}}

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.