One, the monitoring home keyboard, the Android home key System is responsible for monitoring, after capturing the system automatic processing. Sometimes, we need to listen to the home key to handle our own logic, listening to the following methods:
/** * Listen to the home key broadcast*/ PrivateFinalStaticBroadcastreceiver Homelistenerreceiver =NewBroadcastreceiver () {final String System_dialog_reason_key="reason"; Final String System_dialog_reason_home_key="HomeKey"; @Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (intent.action_close_system_dialogs)) {String reason=Intent.getstringextra (System_dialog_reason_key); if(Reason! =NULL&&reason.equals (System_dialog_reason_home_key)) { //handle your own logic. } } } };
New Intentfilter (intent.action_close_system_dialogs); Context.registerreceiver (Homelistenerreceiver, HomeFilter) ;
Anti-register receiver when view disappears // Anti-registration home monitor if NULL ) { context.unregisterreceiver (homelistenerreceiver);}
Second, the WindowManager view in the monitoring and return and the menu key
rewrite dispatchkeyevent @Override PublicBoolean dispatchkeyevent (KeyEventEvent) { Switch(Event. getKeyCode ()) { CaseKeyevent.keycode_back: CaseKeyevent.keycode_menu://Handle your own logic Break; default: Break; } returnSuper.dispatchkeyevent (Event); }