Android applications keep waking up in the background (implemented using wakelock)

When using some product columns, such as wechat and QQ, if there is a new message, the mobile phone screen will light up and prompt a sound even when the screen is locked. At this time, the user will know that a new message is coming. However, generally, after the mobile phone locks the screen, in order to save power and reduce CPU consumption, the Android system will put the system into sleep after a period of time. At this time, the CPU in the Android system will remain in a relatively low power consumption state. For the previous example, there must be a network request when a new message is received, and the network request is an operation that consumes CPU. So how to maintain the network state of the system and wake up the mobile phone through the program after the screen lock state and even the system enters sleep? The answer is the wakelock mechanism in Android.

First 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 is responsible for managing 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 holds this, the system cannot enter the sleep stage. Since you want to keep the application running in the background all the time, you naturally need to obtain this lock to ensure that the application runs in the background all the time. I made a request to run a service in the background to perform polling, but it was found that after a period of time, the polling was interrupted (I requested to stop after 20 minutes). However, after unlocking the screen again, the polling request started again. Later, I tried the usage of wakelock found on stackoverflow and it worked very well. Before using this method, I set the service as the foreground service and other methods did not work. I wonder if there is a better way to meet this demand. You can leave a message and discuss it with me!

Next, see how to use wakelock:

The first method above is to obtain the lock, and the second method is to release the lock. Once the lock is obtained, the system background can keep the application that obtains the lock running after the screen is turned off or the lock screen is turned off for a long time. After obtaining the instance PM of PowerManager, obtain the instance of wakelock through the newwakelock method. The first parameter specifies which type of lock to obtain. Different locks have different effects on the system CPU, screen and keyboard, and the second parameter is the user-defined name.

Effects of various lock types on 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. It is allowed to keep the screen display, but it may be gray. It is allowed to turn off the keyboard light

SCREEN_ BRIGHT_ WAKE_ Lock: keep the CPU running, allow the screen to be highlighted, and allow the keyboard light to be turned off

FULL_ WAKE_ Lock: keep the CPU running, keep the screen highlighted, and keep the keyboard light bright

ACQUIRE_ CAUSES_ Wakeup: force the screen to light up. This lock is mainly for some operations that must be notified to the user

ON_ AFTER_ Release: keep the screen on for a period of time when the lock is released

Finally, don't forget to declare permissions:

The above summarizes a method for a demand. Friends with better methods hope to put forward it in the message and discuss the improvement together. Thank you!

Requirement: to run a service in the background to perform polling, you still need to keep the service in the polling state after the screen is off or locked.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>