Android Press two times physical return key to exit the program

Source: Internet
Author: User

<?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

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.