Android presses the physical return key twice to exit the program, and android twice.

Source: Internet
Author: User

Android presses the physical return key twice to exit the program, and android twice.

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Define the current layout of the basic LinearLayout --> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <! -- Define the text tab of the page --> <TextView android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: text = "click the return key twice in 3 seconds, exit the program ..... "/> </LinearLayout>

 

Package com. example. yanlei. yl2; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. support. v7.app. appCompatActivity; import android. view. keyEvent; import android. widget. toast; public class MainActivity extends AppCompatActivity {// indicates whether to exit the program. The flag private boolean isExit = false; // defines the handler private Handler mHandler = new Handler () that accepts the information sent by the user () {@ Override public void handl EMessage (Message msg) {super. handleMessage (msg); // indicates that the user does not exit the isExit = false; }}; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // listens to the mobile phone's physical key click event @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// determine whether the user clicks the return key if (keyCode = KeyEvent. KEYCODE_BACK) {// if isExit is marked as false, the user is prompted to press if (! IsExit) {isExit = true; Toast. makeText (getApplicationContext (), "exit the program again", Toast. LENGTH_SHORT ). show (); // if the user does not press the return key again within 2 seconds, the message will be sent to mark the user as not exiting mHandler. sendEmptyMessageDelayed (0, 3000);} // If isExit is marked as true, exit the else program {// exit the program finish (); System. exit (0) ;}} return false ;}}

 

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.