How to capture back key events on Android platform, back key is the return button on the phone, the general software does not capture relevant information may cause your program to be switched to the background, and the awkward situation of returning to the desktop, there are two ways on Android to get the event of the button.
1. Direct access to the button press event, this method is compatible with Android 1.0 to Android 2.1 is also the general method, directly rewrite the activity of the OnKeyDown method, the code is as follows:
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {//pressed if it is back and not duplicated
Toast.maketext (Ml78.this, "Magic go back Key test", 1). Show ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
For Android 2.0, there is a new method, for the activity can be separately get back key press event, directly rewrite the onbackpressed method, the code is as follows
@Override
public void onbackpressed () {
Here's the logic code, People Note: This method is only available for 2.0 or newer SDK
Return
}
Introduction to the use of Android back key onbackpressed ()