WakeLocks are a Power Manager system Service feature that your application can use to control the power state of the host device.Wake Locks can be used to keep the CPU running, prevent the screen from dimming, prevent the screen from turning off, and prevent the keyboard backlight from turning off.Creating and holding Wake Locks can have a dramatic impact on the host device’s battery life. It’s good practice to use Wake Locks sparingly, creating them only when strictly necessary and holding them for as short a time as possible.Because of the dramatic impact Wake Locks can have on battery life, your application needs to request a wake_lock permission in order to create them:To create a Wake Lock, call newWakeLock on the Power Manager, specifying one of the following Wake Lock types.^ full_wake_lock — Keeps the screen at full brightness, the keyboard backlight illuminated,and the CPU running.^ screen_bright_wake_lock — Keeps the screen at full brightness and the CPU running. ► screen_dim_wake_lock — Keeps the screen on (but lets it dim) and the CPU running.► partial_wake_lock — Keeps the CPU running.Screen dim Wake Locks typically are used to prevent the screen from dimming during applications that are likely to involve little user interaction — for example, a video player.Partial Wake Locks (or CPU Wake Locks) are used to prevent the device from going to sleep until an action has completed. This is most commonly used by Services started within Intent Receivers, which may receive Intents while the device is asleep. It’s worth noting that in this case the system will hold a CPU Wake Lock throughout the onReceive handler of the Broadcast Receiver.If you start a Service, or broadcast an Intent within the onReceive handler of a Broadcast Receiver, it is possible that the Wake Lock it holds will be released before your Service has started or your Intent received. To ensure the Service exe¬cution is completed, you will need to put in place a separate Wake Lock policy.
After creating a Wake Lock, acquire it by calling acquire.
You can optionally specify a timeout to ensure the maximum duration the Wake Lock will be held for. When the action for which you’re holding the Wake Lock completes, call release to let the system manage the power state.
Listing 18-12 shows the typical use pattern for creating, acquiring, and releasing a Wake Lock.
đang được dịch, vui lòng đợi..
