Two implementations of the Android listener return key Exit program

Source: Internet
Author: User

1,Android Double-click Back button to exit the program

Idea: When the user presses the return key, a timer is set to monitor whether the exit is realized within 2 seconds, and if the user does not press the back key again, clears the effect of pressing the return key for the first time so that the program reverts to the state before the first press of the return key. The timer is created every time the user presses the Back button.

The code is as follows:

    /*** menu, return key response*/@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        //TODO auto-generated Method Stub        if(KeyCode = =keyevent.keycode_back)        {Exitby2click (); //call the double Exit function        }        return false; }    /*** Double Exit function*/    Private StaticBoolean Isexit =false; Private voidExitby2click () {Timer texit=NULL; if(Isexit = =false) {Isexit=true;//Ready to ExitToast.maketext ( This, "Press again to exit the program", Toast.length_short). Show (); Texit=NewTimer (); Texit.schedule (NewTimerTask () {@Override Public voidrun () {Isexit=false;//Cancel Exit                }            }, 2000);//If the return key is not pressed within 2 seconds, the start timer cancels out the task just performed        } Else{finish (); System.exit (0); }    }

where onkeydown () function can be found in the menu bar source->override/implement methods, double-click can be automatically added to the code.

2. Listen for return key, pop up an exit dialog box

How Android listens for the return key click event and creates an exit dialog box,

Prevent yourself from writing an application that accidentally clicks the exit key and exits directly.

The source code is as follows:

 Public classBackkeytestextendsactivity{/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        if(KeyCode = =keyevent.keycode_back) {            //Create exit dialog boxAlertdialog Isexit =NewAlertdialog.builder ( This). Create (); //Setting the dialog box titleIsexit.settitle ("System Prompt"); //Setting up dialog box messagesIsexit.setmessage ("OK to exit"?)); //Add a Select button and register for monitoringIsexit.setbutton ("OK", listener); Isexit.setbutton2 (Cancel, listener); //Show dialog Boxisexit.show (); }                return false; }    /**Listen for button click Events in dialog Box*/Dialoginterface.onclicklistener Listener=NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intwhich) {            Switch(which) { CaseAlertdialog.button_positive://"Confirm" button to exit the programfinish ();  Break;  CaseAlertdialog.button_negative://Cancel a second button Cancel dialog box                 Break; default:                 Break;    }        }    }; }

Two implementations of the Android listener return key Exit 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.