The status of the listening screen needs to be broadcast, and we must also dynamically register the broadcast to enable the monitor screen to be dimmed (invalid static registration). I am here in the service to do the processing, no technical difficulty, directly on the code.
PackageCom.kale.camerademo;ImportAndroid.app.Service;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.IBinder;/** * @author: * @tips: * @web:http://blog.csdn.net/lamp_zy/article/details/7968938* @date: 2014-2-27*/ Public classScreenserviceextendsService {@Override Publicibinder onbind (Intent arg0) {return NULL; } Public voidonCreate () {Super. OnCreate (); /*registering a broadcast when the screen wakes up*/Intentfilter Mscreenonfilter=NewIntentfilter ("Android.intent.action.SCREEN_ON"); Screenservice. This. Registerreceiver (Mscreenoreceiver, Mscreenonfilter); /*broadcast when registering a machine lock screen*/Intentfilter Mscreenofffilter=NewIntentfilter ("Android.intent.action.SCREEN_OFF"); Screenservice. This. Registerreceiver (Mscreenoreceiver, Mscreenofffilter); } Public voidOnDestroy () {Super. OnDestroy (); Screenservice. This. Unregisterreceiver (Mscreenoreceiver); } /*** Lock Screen management class called Keyguardmanager, * by calling its internal class Keyguardlockmkeyguardlock object Disablekeyguard method can cancel the system lock screen, * Newkeyguard The lock parameter is used to identify who is hiding the system lock screen*/ PrivateBroadcastreceiver Mscreenoreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals ("Android.intent.action.SCREEN_ON") {System.out.println ("--screen_on--"); } Else if(Action.equals ("Android.intent.action.SCREEN_OFF") {System.out.println ("--screen_off--"); } } };}
Start the service:
StartService (new Intent (mainactivity. this, Screenservice. class));
Operation Result:
Monitor on-screen light and off status