Section Introduction:
This section explains one of the system services provided by Android—PowerManager (Power Service), which is used to manage CPU operation and keep the keyboard or screen lit; however, unless absolutely necessary, try not to use this class. If you do use it, be sure to release it promptly! This section will not go too deeply into this class, because it involves some low-level stuff. When we need to use it later, we'll study it in depth~ This section mainly introduces some basic concepts: PowerManager, the WakeLock mechanism, and so on!
Official API documentation:PowerManager
1. What is PowerManager
It is a power management API provided by the Android system. Its related interfaces are closely related to the device battery life. As the official docs say, unless absolutely necessary, you should try to avoid using this class, and after using it, be sure to release it promptly!
The so-called power management includes: CPU operation, and the keyboard or screen lighting up! The core is actually theWakeLockmechanism. As long as we hold this lock, the system cannot enter the sleep state, and it can be acquired by user-mode programs or the kernel! The lock can be:"with timeout" or "without timeout". A timeout lock will automatically unlock when the time is up. If there is no lock or it times out, the kernel will start the sleep mechanism to enter sleep mode!
2. Introduction to the WakeLock
PowerManager.WakeLockYesLockingandUnlockingtwo states. And there are two forms of locking:
①Permanent lock, this kind of lock will not unlock unless explicitly released, so you need to be very careful when using it!
②Timeout lock, it will unlock when the time is up. And after creating a WakeLock, there aretwo locking mechanisms: ① Non-counting lock mechanism,② Counting lock mechanism (default)This can be specified viasetReferenceCounted(boolean value). The difference is:acquireNo matter how many times ( ) is called, a singlerelease( ) can unlock the lock.(--count == 0)The latter requires ( ) calls; likewise, only when(count == 0)will it actually acquire/release the lock.WakeLockThe counting mechanism does not truly acquire/release a lock for every request;it only counts the number of times the same lock has beenacquired/released, and then operates based on that!
ps: More in-depth content involves low-level details. The author's level is limited, and I haven't reached that level yet, so I won't study it in depth here. I'll just cover the basics. If it is needed later, I'll write a separate 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 above-mentionednewWakeLockfirst ( )flag, these flags affect system power to varying degrees.
These flags are exclusive, and only one of them can be specified at a time.
PARTIAL_WAKE_LOCK: Keep the CPU running; the screen and keyboard light may be turned off.
SCREEN_DIM_WAKE_LOCK: Keep the CPU running; the screen is allowed to stay on but may be dimmed, and the keyboard light is allowed to be turned off.
SCREEN_BRIGHT_WAKE_LOCK: Keep the CPU running; the screen is allowed to stay bright, and the keyboard light is allowed to be turned off.
FULL_WAKE_LOCK: Keep the CPU running; keep the screen bright, and the keyboard light stays on as well.
ps: If you are using a partial wake lock (using thePARTIAL_WAKE_LOCKflag),PARTIAL_WAKE_LOCKthe CPU will continue to run, ignoring any timers, even pressing the power button. With other wake locks, the CPU also continues to run, but the user can still press the power button to make the device sleep. In addition, you can use more than one flag, but they only affect the behavior of the screen.
and
ACQUIRE_CAUSES_WAKEUPwhen used together, there is no effect.
: A normal wake lock does not actually turn on the lighting. Instead, once it is turned on, it will remain on (e.g., from user activity). When the wake lock is acquired, this flag will cause the screen and/or keyboard to turn on immediately.
ON_AFTER_RELEASE: Setting this flag will reset the user activity timer when the wake lock is released, causing the lighting to last for a period of time. If you are looping in a wake lock state, this can be used to reduce flicker.
4. Required Permissions
To perform power operations, you need to declare in AndroidManifest.xml that the application has the permission to manage power:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
You may also need:
<uses-permission android:name="android.permission.DEVICE_POWER"/>
In addition, WakeLock settings areActivity-level, not for the entire Application!
Section Summary:
Okay, this section introduced PowerManager (Power Service), but it is only for general understanding. The content also mentioned that try not to use this class unless absolutely necessary~ It doesn't matter whether you understood it or not, just be aware of it!