Need to configure manifest file: <uses-permission android:name= "Android.permission.WAKE_LOCK"/>
You can also refer to this article I wrote earlier:
http://blog.csdn.net/aikongmeng/article/details/39232017
Package Com.example.wakeup;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.sensormanager;import Android.os.Bundle;import Android.os.handler;import Android.os.powermanager;import Android.util.log;import Android.widget.TextView;public Class Mainactivity extends Activity implements sensoreventlistener{private static final String TAG = "Mainactivity";p Riva Te static final String Wake_lock_tag = "Lighttag";p rivate powermanager pm;p rivate powermanager.wakelock wl;private TextVi EW tv;private StringBuilder mbuilder = new StringBuilder (2048);p rivate Sensormanager msensormanager;private Sensor Mlight;private Handler Handler =new Handler (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {SUPER.ONCREate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); Msensormanager = (Sensormanager) getsystemservice (sensor_service); mlight = Msensormanager.getdefaultsensor ( Sensor.type_light);} @Overrideprotected void Onresume () {super.onresume ();p m = (PowerManager) getsystemservice (context.power_service); wl = Pm.newwakelock (Powermanager.partial_wake_lock, Wake_lock_tag); Wl.acquire (); } @Overrideprotected void OnStart () {Super.onstart (); Msensormanager.registerlistener (this, mlight, Sensormanager.sensor_delay_fastest);//register broadcasts Receiverintentfilter filter = new Intentfilter ( Intent.action_screen_off); Registerreceiver (mreceiver, filter); } @Overrideprotected void OnPause () {super.onpause ();//release Lockif (wl!=null) {wl.release (); wl=null;}} @Overrideprotected void OnDestroy () {Super.ondestroy (); Msensormanager.unregisterlistener (this); Unregisterreceiver (Mreceiver); } @Overridepublic void Onaccuracychanged (sensor sensor, int accuracy) {} @Overridepublic void Onsensorchanged (Sensorevent event) {log.i (TAG, "light Values:" + event.values[0]); Mbuilder.insert (0, event.values[0]+ "\ n"); Tv.settext (mbuilder.tostring ()); Tv.invalidate (); }private broadcastreceiver mreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent in Tent) {if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase (Intent.getaction ())) {Handler.post (new Runnable () {@ Suppresslint ("Wakelock") public void Run () {if (WL! = null) {wl.release (); Wl=null;} try {System.out.println ("sleeping ..."); Thread.Sleep (3000);} catch (Interruptedexception e) {e.printstacktrace ();} WL = Pm.newwakelock (Powermanager.partial_wake_lock, Wake_lock_tag); Wl.acquire (); } }); } }};}
Android Wake Lock Power Management Simple Learning