This example describes the implementation of the Android return key function. Share to everyone for your reference. Specifically as follows:
In the development of Android applications, often by pressing the return key (that is, keycode = = Keyevent.keycode_back) can close the program, in fact, most of the case did not turn off the application
We can do this, when the user clicks the Custom exit button or returns the key (needs to capture the action), we forcibly exits the application in the OnDestroy (), or kills the process directly, the concrete operation code is as follows:
public boolean onKeyDown (int keycode, keyevent event) {
//press the return button on the keyboard
if (keycode = = keyevent.keycode_back) {
New Alertdialog.builder (This)
. Setmessage (OK to exit the system?) ")
. Setnegativebutton (" Cancel ",
new Dialoginterface.onclicklistener () {public
void OnClick ( Dialoginterface dialog,
int which) {
}
}
. Setpositivebutton (OK),
new Dialoginterface.onclicklistener () {public
void OnClick (Dialoginterface dialog,
int whichbutton) {
Finish ();
}
}). Show ();
return true;
} else {return
Super.onkeydown (KeyCode, event);
}
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Or the following way
//system.exit (0);
It is recommended to use this
android.os.Process.killProcess (Android.os.Process.myPid ());
}
I hope this article will help you with your Android program.