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 ;}}