import android.app.service;import android.content.context;import android.os.powermanager;/** * wake lock is a mechanism of locking, as long as someone holding this lock, the system will not be able to enter the sleep, * can be user-state program and the kernel to obtain . This lock can either be timed out or is not timed out, * the lock will automatically unlock after the time elapses . if there is no lock or timeout, The kernel will * start the sleep mechanism to go into hibernation. * * , about the int flags of various types of locks on cpu , screen, keyboard effects: * 1.partial_wake_ LOCK: Keep the CPU running, and the screen and keyboard lights may be off. * 2.screen_dim_wake_lock: Keep cpu running, allow screen display but may be gray, allow the keyboard light to turn off * 3.screen_bright_ Wake_lock: Keep the cpu running, allow the screen to be highlighted, allow the keyboard light to turn off * 4.full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard light to keep the brightness * 5.acquire_causes_wakeup:normal wake locks don ' T * actually turn on the illumination. instead, they cause the illumination to * remain on once it turns on (e.g. from user activity) . this flag will force * the screen and/or keyboard to turn on immediately, when the wakelock is * acquired. a typical use would be for notifications which are important for * the User to see immediately. * 6.on_after_release:f this flag is set, the user * activity timer will be reset when the WakeLock is released, causing the * illumination to remain On a bit longer. this can be used to reduce flicker if * you are cycling between wake lock conditions. * * You can add more than two logos, which can only affect the behavior of the screen. These flags will have no effect when there is a partial_wake_lock in the combo. * * second, access to power to be operated on the ANDROIDMANIFEST.XML declares that the app has permission to set up power management. * <uses-permission android:name= "Android.permission.WAKE_LOCK" /> * you may also need to * <uses-permission android:name= "Android.permission.DEVICE_POWER" / > * * wakelock settings are Activiy levels, not for the entire application application. * **/abstract class WakeLockService extends Service{ private android.os.PowerManager.WakeLock mWakeLock; wakelockservice () { } /** * get lock to keep screen brightness. In android the power supply is controlled by various lock locks, it is important to note that the lock and unlock must appear in pairs. * General use: This function is called in the onresume of the activity. The Releasewakelock () method frees the lock and is called in the activity's onpause. */ protected void acquirewakelock () { if (mwakelock == null) { / /Wakelock instances are generated by PowerManager's Newwakelock ((Int flags, string tag) method. //int flags indicates what kind of wakelock to get, Different lock has different effects on CPU, screen and keyboard lights. PowerManager pm = ( PowerManager) getsystemservice (context.power_service); mwakelock = pm.newwakelock (Powermanager.screen_dim_wake_lock, getclass ( ). Getcanonicalname ()); Mwakelock.setreferencecounted (False); Mwakelock.acquire (); } } protected void acquirewakelock (long timeout) { if (mwakelock == null) { PowerManager pm = ( PowerManager) getsystemservice (context.power_service); mwakelock = pm.newwakelock (Powermanager.screen_dim_wake_lock, getclass ( ). Getcanonicalname ()); Mwakelock.setreferencecounted (False); Mwakelock.acquire (timeout); } } /** * release lock, shows the release, if the requested lock does not release the system will not go into hibernation. */ protected void releasewakelock () {if ( mwakelock == null | | !mwakelock.isheld ()) { mwakelock.release (); mwakelock = null;}}}
This article from the "Sky no traces but I flew" blog, please be sure to keep this source http://glblong.blog.51cto.com/3058613/1610983
Android Note: Wake Lock