Android uses WakeLock

Source: Internet
Author: User

To prolong the battery life, the Android device fades the screen after a period of time, closes the screen display, and stops the CPU. WakeLock is a power management system service function that applications can use to control the power status of a device.

WakeLock can be used to keep the CPU running, prevent the screen from going dark or off, and prevent the keyboard backlight from going out.

Note:

Creating and using a Wake Lock can significantly affect the battery usage of applications. Therefore, it is best

Use the Wake Lock if necessary, and release them as long as they are available.

Screen Wake Lock is usually used to prevent the screen from being dimmed during a user's viewing of the screen but rarely interacts with the screen (for example, playing a video.

The CPU Wake Lock is used to prevent the device from sleep until an operation is performed. This often happens when the service is started from the intent receiver because the intent receiver may receive the intent during device sleep. It is worth noting that in this case, the system will use the CPU Wake Lock throughout the onReceive handler of the broadcast receiver.

Note:

If you enable a service or broadcast an intent in the onReceive handler of the broadcast receiver, it is possible

Before starting, the used Wake Lock will be released. To ensure that the service can be executed, an independent Wake Lock is required.


Steps:

  1. PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );Context.getSystemService()Method to obtain the PowerManager instance.
  2. Then, use the PowerManager newWakeLock (int flags, String tag) to generate a WakeLock instance. Int Flags indicates the type of WakeLock to be obtained. Different locks have different effects on cpu, screen, and keyboard lights.
  3. After obtaining the WakeLock instance, obtain the corresponding lock through acquire (), perform other business logic operations, and finally use release () to release (release is required ).

    About flags

    The impact of various lock types on the CPU, screen, and keyboard:

    PARTIAL_WAKE_LOCK: to keep the CPU running, the screen and keyboard lights may be disabled.

    SCREEN_DIM_WAKE_LOCK: keeps the CPU running, allows screen display, but may be gray, allows the keyboard light to be turned off

    SCREEN_BRIGHT_WAKE_LOCK: keeps the CPU running, allows screen highlighting, and allows you to turn off the keyboard light

    FULL_WAKE_LOCK: keeps the CPU running, keeps the screen highlighted, and keeps the keyboard light 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 will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use wocould be for communications which are important for the user to see immediately.

    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 using ing between wake lock conditions.

    Permission acquisition

    To perform power operations, you must declare in AndroidManifest. xml that the application has the permission to set power management.


    You may need

    In addition, the settings of WakeLock are Activiy-level, not for the entire Application.

    You can operate WakeLock in the onResume method of the activity and release it in the onPause method.


    Example:

    public abstract class WakeLocker {    private static PowerManager.WakeLock wakeLock;    private static final String APP_TAG = "MyWakeLock";    public static void acquire(Context ctx) {        if (wakeLock != null)        wakeLock.release();/*        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |                PowerManager.ACQUIRE_CAUSES_WAKEUP |                PowerManager.ON_AFTER_RELEASE|                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, APP_TAG);*/        PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, APP_TAG);                        wakeLock.acquire();    }public static void release() {        if (wakeLock != null)         wakeLock.release();         wakeLock = null;    }}


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.