PowerManager (power service)


Introduction to this section:

This section will explain the system services provided by Android - PowerManager (power service), used for Manage the CPU to run and the keyboard or screen to light up; however, do not use this class unless you have to. If After you use it, be sure to release it in time! This section will not explain this B in too much depth, because it involves some underlying Things that we will need to study in depth in the future ~ This section mainly introduces some basic concepts, PowerManager, wakelock Lock mechanism and more!

Official API document: PowerManager


1.What is PowerManager


Android system for us Provides an API for power management, and its related interfaces are closely related to the battery life of the device. The official also said that unless it is absolutely necessary, you should try to avoid using this class and release it in time after use!

The so-called power management includes: CPU running, keyboard or screen lighting up! The core is actually the wakelock lock mechanism. As long as we hold this lock, Then the system cannot enter the sleep state and can be obtained by the user mode program or the kernel! The lock can be: "with timeout" or "No timeout", the timeout lock will be automatically unlocked after the time expires. If there is no lock or timeout, the kernel will start the sleep mechanism to enter sleep!


2 .wakelock lock introduction


PowerManager.WakeLock has two states: locked and unlocked, and locked There are two forms:

Permanently locks . This lock will not be unlocked unless explicitly released, so you need to be very careful when using it!

Timeout lock will be unlocked after the time is up. After creating WakeLock, there are two locking mechanisms:①Uncounted lock mechanism, ② Counting lock mechanism (default) can be specified by setReferenceCounted(boolean value), the difference is: In the former, no matter how many times acquire( ), one release( ) can unlock the lock. The latter requires (--count == 0), and it will also apply for the lock when (count == 0) Therefore, the counting mechanism of WakeLock does not apply for/release a lock for each request in the true sense; Just count the number of times the same lock has been applied/released, and then proceed!

ps: Regarding the more in-depth content, it involves the underlying content. The author’s level is limited and I haven’t reached that level yet. I won’t go into in-depth research here, just talk about some basics. If necessary in the future, I will write another article~


3. How to use PowerManager


PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock w1 = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyTag");
w1.acquire();
//在这个过程,屏幕会保持光亮!
w1.release();

The first flag mark of the above newWakeLock( ), these flags affect the system power supply to varying degrees.

These tags are exclusive and only one of them can be specified at a time.

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

SCREEN_DIM_WAKE_LOCK: Keep the CPU running, allow to keep the screen display but may be gray, allow to turn off the keyboard light

SCREEN_BRIGHT_WAKE_LOCK: Keep the CPU running , allow to keep the screen highlighted, allow to turn off the keyboard light

FULL_WAKE_LOCK: Keep the CPU running, keep the screen highlighted, and the keyboard light also maintains brightness

ps: If you are using a partial wake lock (using the PARTIAL_WAKE_LOCK flag), the CPU will continue to run, Any timers will be ignored, even pressing the power button. In other wake lock cases, the CPU will continue to run, but the user will still You can then press the power button to put the device to sleep. Alternatively, you can use more than two markers, but they only affect the behavior of the screen. If used together with PARTIAL_WAKE_LOCK, it will have no effect.

Screen unlock parameters:

ACQUIRE_CAUSES_WAKEUP: A normal wake lock does not actually turn on the lighting. Instead, once opened they will always remain Maintain (such as the user's activity in the next life). When wakelock is acquired, this flag causes the screen and/or keyboard to turn on immediately.

A typical use is to immediately see notifications that are important to the user.

ON_AFTER_RELEASE: If this flag is set, the user activity timer will be reset when wakelock is released, causing lighting continue for a period of time. If you are looping in a wacklock condition, this can be used to reduce flickering


4. Required permissions


To perform power operations, you need to set it in AndroidManifest.xml It is stated in that the application has permission to set power management:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

You may also need:

<uses-permission android:name="android.permission.DEVICE_POWER"/>

In addition, the WakeLock setting is Activity level, not for the entire Application Applied!


Summary of this section:

Okay, this section introduces PowerManager (power service), but it is just a popular science, and the content is also explained. Don’t use this class unless you have no choice~ It doesn’t matter if you understand it or not, just know it!