Procedures for PowerManager and Wakelock

Source: Internet
Author: User
procedures for PowerManager and Wakelock

Transferred from: http://www.cnblogs.com/GnagWang/archive/2011/02/27/1966611.html

PowerManager pm = (powermanager) getsystemservice (Context.power_service), via Context.getsystemservice (). method to get the PowerManager instance. The Wakelock instance is then generated by PowerManager's newwakelock (int flags, String tag). The INT flags indicates which wakelock to get, and the different lock has different effects on the CPU, screen, keyboard lights. Gets the Wakelock instance, obtains the corresponding lock through acquire (), then carries out the operation of the other business logic, and finally releases (release is required) using release ().

Wakelock.acquire (); Wake-up Light screen

The screen will be lit during this period.

Wakelock.release (); Restore the screen to the dark

Of course, Android security does not mean that the developer has the authority, you can control the screen backlight display or none, only through the acquire lit backlight to use release to turn off the backlight, if the direct call to the release method to close the screen will produce an exception.

Starting from the Android 2.1 API LEVEL7, you can use the public boolean Isscreenon () method to determine whether the screen is lit or not, and the code is

PowerManager pm = (powermanager) getsystemservice (Context.power_service);
Boolean Isscreenon = Pm.isscreenon (); about INT Flags

The effects of various types of locks on the CPU, screen, and keyboard:

partial_wake_lock: Keep the CPU running, and the screen and keyboard lights may be off.

screen_dim_wake_lock: Keep the CPU running, allow the screen to be displayed but may be gray, allow to turn off the keyboard light

screen_bright_wake_lock: Keeps the CPU running, allowing the screen to be highlighted, allowing the keyboard light to be turned off

full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard lights remain bright

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 would force the screens and/or keyboard to turn on immediately and when the WakeLock is acquired. A Typical use would is for notifications which is important for the user to see immediately.

On_after_release: F This flag is set, the user activity timer would be reset if the WakeLock is released, causing The illumination to remain on a bit longer. This can is used to reduce flicker if you is cycling between wake lock conditions. Permission Acquisition

The operation to be powered requires that the app has permission to set power management in Androidmanifest.xml.

<uses-permission android:name= "Android.permission.WAKE_LOCK"/> You may also need <uses-permission android:name= " Android.permission.DEVICE_POWER "/>

The other Wakelock settings are Activiy level, not for the entire application application.

The wakelock can be manipulated within the Onresume method of the activity and released within the OnPause method.

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.