For unlocking and lock screen This use of the more frequent action, Google has made restrictions, must hand code registration
Directly on the code: This is a registered broadcast
1 Public classMainactivityextendsappcompatactivity {2 Screen screen ;3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7 8Screen =NewScreen ();9Intentfilter filter =NewIntentfilter ();Ten //Add Action OneFilter.addaction ("Android.intent.action.SCREEN_OFF"); AFilter.addaction ("Android.intent.action.SCREEN_ON"); - //register a broadcast recipient - This. Registerreceiver (screen,filter); the } - - @Override - protected voidOnDestroy () { + Super. OnDestroy (); - unregisterreceiver (screen); +}
1 Public classScreenextendsBroadcastreceiver {2 @Override3 Public voidOnReceive (Context context, Intent Intent) {4String action =intent.getaction ();5 if(Action.equals ("Android.intent.action.SCREEN_OFF")){6SYSTEM.OUT.PRINTLN ("Screen lock screen up");7}Else if(Action.equals ("Android.intent.action.SCREEN_ON")){8SYSTEM.OUT.PRINTLN ("Screen unlocked");9 }Ten } One}
Broadcastreceiver screen lock screen and unlock monitor