Android Wake Lock Power Management Simple Learning

Source: Internet
Author: User

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

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.