<?XML version= "1.0" encoding= "Utf-8"?><!--define the basic linearlayout of the current layout -<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <!--Define page text labels - <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:text= "You can exit the program by tapping two times within 3 seconds ..." /></LinearLayout>
Packagecom.example.yanlei.yl2;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.support.v7.app.AppCompatActivity;Importandroid.view.KeyEvent;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {//defines whether to exit the program's markup Private Booleanisexit=false; //defines the handler that accepts a user's message PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //flag user does not exit statusisexit=false; } }; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } //Monitor your phone's physical keystroke click events@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //determine if the user clicked the return key if(KeyCode = =keyevent.keycode_back) { //if Isexit is marked false, prompt the user to press the key again if(!isexit) {Isexit=true; Toast.maketext (Getapplicationcontext (),"Press one more time to exit the program", Toast.length_short). Show (); //If the user does not press the return key again within 2 seconds, the message is flagged to the user without exiting the stateMhandler.sendemptymessagedelayed (0, 3000); } //if Isexit is marked True, exit the program Else{ //Exit Programfinish (); System.exit (0); } } return false; }}
Android Press two times physical return key to exit the program