Android back home key Monitor
Back key Monitoring
The back key is easier to listen to, and can be intercepted in multiple systems, such as the following events in the activity:
@Override Public voidonbackpressed () {//super.onbackpressed ();//comment out this line, back key does not exit activitylog.i (Log_tag,"Onbackpressed"); } @Override Public Booleandispatchkeyevent (KeyEvent event) {log.i (Log_tag,"Dispatchkeyevent:keycode--" +Event.getkeycode ()); return Super. Dispatchkeyevent (event); } @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {log.i (Log_tag,"Onkeydown:keycode--" +keycode); Switch(keycode) { Casekeyevent.keycode_back:log.i (Log_tag,"Keyevent.keycode_back"); Break; Casekeyevent.keycode_menu:log.i (Log_tag,"Keyevent.keycode_menu"); Break; Casekeyevent.keycode_home:log.i (Log_tag,"Keyevent.keycode_home"); //not receiving Break; Casekeyevent.keycode_app_switch:log.i (Log_tag,"Keyevent.keycode_app_switch"); //not receiving Break; default: Break; } return Super. OnKeyDown (KeyCode, event); }
Home key for broadcast monitoring
The home key is not so easy to listen to, because the home key can put the program out of the background, so this event is directly distributed to the system, the system received after the corresponding processing, the home key event is not directly passed to the application inside. So in the above code to listen back key, The corresponding callback is an event that does not receive the home key .
Refer to the blog after the link, the home key monitoring through the registration of the broadcast receiver to achieve , intercept the window to shut down the system action, and then according to the specific parameters inside the intent, analysis of the current home key, the application of the switch, or other function keys.
The receiver implementation is as follows:
PackageCom.mengdd.hellohome;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classHomewatcherreceiverextendsBroadcastreceiver {Private Static FinalString Log_tag = "Homereceiver"; Private Static FinalString System_dialog_reason_key = "REASON"; Private Static FinalString System_dialog_reason_recent_apps = "Recentapps"; Private Static FinalString System_dialog_reason_home_key = "HomeKey"; Private Static FinalString System_dialog_reason_lock = "LOCK"; Private Static FinalString system_dialog_reason_assist = "ASSIST"; @Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); LOG.I (Log_tag,"Onreceive:action:" +action); if(Action.equals (intent.action_close_system_dialogs)) {//Android.intent.action.CLOSE_SYSTEM_DIALOGSString reason =Intent.getstringextra (System_dialog_reason_key); LOG.I (Log_tag,"Reason:" +reason); if(System_dialog_reason_home_key.equals (REASON)) {//Press the home key brieflyLOG.I (Log_tag, "HomeKey"); } Else if(System_dialog_reason_recent_apps.equals (REASON)) {//Press and hold the home button or the activity toggle keyLOG.I (Log_tag, "long press home key or activity switch"); } Else if(System_dialog_reason_lock.equals (REASON)) {//Lock ScreenLOG.I (Log_tag, "lock"); } Else if(System_dialog_reason_assist.equals (REASON)) {//Samsung Long Press the home buttonLOG.I (Log_tag, "assist"); } } }}
Note that different phone keys are different, so you need to make a distinction between different reasons.
Home key monitoring broadcast registration
There are two ways to register a broadcast receiver, one is static registration, which is written in manifest, and the other is dynamic registration, which is registered in Java code.
The receiver that listens to the home key above, statically registers as follows:
< receiver android:name = "Com.mengdd.hellohome.HomeWatcherReceiver" > < intent-filter > < action android:name = "Android.intent.action.CLOSE_SYSTEM_DIALOGS" /> </ intent-filter > </ receiver >
However, it is found that static registration does not work , that is, the OnReceive callback is not received.
Use dynamic registration:
Private StaticHomewatcherreceiver Mhomekeyreceiver =NULL; Private Static voidRegisterhomekeyreceiver (Context context) {log.i (Log_tag,"Registerhomekeyreceiver"); Mhomekeyreceiver=NewHomewatcherreceiver (); FinalIntentfilter Homefilter =NewIntentfilter (intent.action_close_system_dialogs); Context.registerreceiver (Mhomekeyreceiver, Homefilter); } Private Static voidUnregisterhomekeyreceiver (Context context) {log.i (Log_tag,"Unregisterhomekeyreceiver"); if(NULL!=mhomekeyreceiver) {context.unregisterreceiver (mhomekeyreceiver); } }
In the Onresume and onpause of the activity are called separately:
@Override protectedvoid Onresume () { super. Onresume (); Registerhomekeyreceiver (this); } @Override protectedvoid onPause () { unregisterhomekeyreceiver ( This ); Super . OnPause (); }
Of course, you can also register and log out at other appropriate times as needed.
References
http://blog.csdn.net/way_ping_li/article/details/8953622
Android Home Key Monitor