In Android development, the monitoring of Return key events is often used, following an example to demonstrate the Android to the return key event monitoring.
public class Backkeytest extends activity{/** called when the Activity is first created. */@Overridepublic void onCreate (Bundle Savedin Stancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {// Create Exit Dialog Alertdialog Isexit = new Alertdialog.builder (this). Create ();//Set Dialog title Isexit.settitle ("System tip");// Set the dialog message Isexit.setmessage ("OK to exit"),///Add Select button and register Listener Isexit.setbutton ("OK", listener); Isexit.setbutton2 ("Cancel", listener);//Display dialog box Isexit.show ();} return false;} /** Listener dialog box button click event */dialoginterface.onclicklistener listener = new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) {switch (which) {case alertdialog.button_positive://"Confirm" button Exit program finish (); Break;case alertdialog.button_negative://"Cancel" the second button cancels the dialog box Break;default:break;}};}
Android phone commonly used three keys, home key, back key and menu key.
In the application we often do a certain amount of processing, convenient for users to use.
First we need to know exactly what the system did when we clicked three keys.
If no listening process is performed,
When you click the Home key, the system only executes the OnStop () method of the activity currently displayed by the application and then jumps out of the interface.
When you click the back key, the system defaults to the finish () method of the application's current activity and then jumps out of the interface.
When you click the menu key, the system does not do any processing by default.
Here is just a simple application demo, we can design a more perfect Exit Program dialog box according to our own needs.
Other processing can also be done after the supervisor hears the return event, and so on.
Android Monitor Return key