Use wakelock to enable the Android Application to wake up in the background

Source: Internet
Author: User

When some products such as QQ and QQ are used, if there is a new message, the screen of the mobile phone will be lit up even when the screen is locked, and the user will know that a new message is coming. However, after the mobile phone lockscreen, the android system will sleep after a period of time to save power and reduce CPU consumption, in the Android system, the CPU remains in a relatively low power consumption state. In the previous example, a network request is required to receive a new message, and the network request consumes CPU. How can we lock the screen and even after the system goes to sleep, keep the network status of the system and passProgramWhat about the wake-up phone? The answer is the wakelock mechanism in Android.

First, let's look at the official explanation:

Powermanager: This
Class gives you control of the power state of the device.

Powermanager. wakelock: Lets
You say that you need to have the device on.

Powermanager manages the power supply of Android devices, and the system controls the power supply through various locks. wakelock is a lock mechanism, as long as someone takes the lock, the system cannot enter the sleep stage. To keep the application running in the background, you must obtain the lock to ensure that the program is always running in the background. Previously, I had a requirement to run a service in the backend to perform round robin. However, after a period of time, the round robin was interrupted (I tested the request to stop 20 minutes later ), but after unlocking the screen again, the polling request started again. Later, I found the wakelock usage on stackoverflow and tried it. It worked quite well. Before using this method, I set the service as a front-end service and other methods did not work. I wonder if there are any better solutions to this requirement. You can leave a message to discuss with me!


Next, let's take a look at how to use wakelock:

Wakelock = NULL; // obtain the power lock to keep the private void acquirewakelock () {If (null = wakelock) running when the service still acquires the CPU when the screen is off) {powermanager PM = (powermanager) This. getsystemservice (context. power_service); wakelock = PM. newwakelock (powermanager. partial_wake_lock | powermanager. on_after_release, "postlocationservice"); If (null! = Wakelock) {wakelock. Acquire () ;}}// release the private void releasewakelock () {If (null! = Wakelock) {wakelock. Release (); wakelock = NULL ;}}

UpperThe first method is to obtain the lock, and the second method is to release the lock. Once the lock is obtained, the screen will be extinguished or locked for a long time, the system background keeps the application programs that have obtained the lock running. After obtaining the PM of the powermanager instance, you can use the newwakelock method to obtain the wakelock instance. The first parameter specifies the type of lock to be obtained, different locks have different effects on the system CPU, screen, and keyboard. The second parameter is the custom name.

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

Partial_wake_lock: Keep the CPU running. The screen and keyboard lights may be disabled.

Screen_dim_wake_lock: Keep the CPU running. It allows screen display but may be gray. You can turn off the keyboard light.

Screen_bright_wake_lock: Keep the CPU running, enable screen highlighting, and disable keyboard lights

Full_wake_lock: Keep the CPU running, screen highlighted, and keyboard light brightness

Acquire_causes_wakeup: Force the screen to light up. This lock mainly applies to operations that must be notified to users.

On_after_release: When the lock is released, keep the screen on for a while


Do not forget to declare permissions:

uses-Permission Android: name = "android. permission. wake_lock " />
uses-Permission Android: name = "android. permission. device_power " />

The above summarizes a method for the proposed requirement. If you have a better method, I hope to raise it in the message and discuss the improvements. Thank you!

Requirements:To run a service in the background for polling, after the screen is off or the screen is locked, you still need to keep the service in the polling status.


FeelArticleUseful to you. Click the forward button in the lower right corner to share it with more people!

At the same time, you are welcome to follow my Sina Weibo chat with me: @ Tang Ren _ Ryan

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.