Project used, record, return key what the physical keys use onkeydown monitoring can be, but lock screen key, home button can not, here use Broadcast monitor both state changes
Intentfilter Fragment:
Mfilter.addaction (intent.action_close_system_dialogs);//home key mfilter.addaction (intent.action_screen_on); Open screen mfilter.addaction (intent.action_screen_off);//Lock Screen mfilter.addaction (intent.action_user_present);// Unlock
Onrecevier Fragment:
public void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (intent.action_close_system_dialogs)) {String reason = Intent.getstringextra (system_dial Og_reason_key); if (reason! = null) {if (Mlistener! = null) {if (Reason.equals (system_dialog _reason_home_key)) {//short press the HOME key mlistener.onhomepressed (); } else if (reason. Equals (System_dialog_reason_recent_apps)) { Long press the Home key mlistener.onhomelongpressed (); }}}} if (Action.equals (intent.action_screen_on)) { Mlistener.onscreenon (); }else if (action.equals (Intent.action_screen_off)) {Mlistener.onscreenoff (); } else{//unlock//todo}
Finally, for the lock screen operation, due to the activity of the first onpause,onstop, so can not be like the previous anti-registration broadcast sample in the OnPause anti-registration, otherwise it will not receive lock screen operation
Android Monitor lock screen key, home button