Monitor on-screen light and off status

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.