Basic Android tutorial-10.6 PowerManager (Power Service)
This section introduces:
This section describes the system services provided by Android --PowerManager), Used
Manage CPU running, and the keyboard or screen is lit up; however, do not use this class unless necessary.
After you use it, you must release it in time! This section will not explain B in depth, because it involves some of the underlying
Things that need to be used in the future ~ This section describes some basic concepts, PowerManager and wakelock.
Lock mechanism, etc!
Official API documentation: PowerManager
1. What is PowerManager?
The Android system provides us with an API for power management. Its interfaces are closely related to the battery endurance of devices,
The official team also said that unless it is a last resort, we 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 on! The core isWakelock lockMechanism, as long as we hold this lock,
Then the system cannot enter the sleep state, which can be obtained by the user State program or kernel! The lock can be :"Timeout"Or
"No timeout", The timeout lock will be automatically unlocked after the time ends. If there is no lock or timeout, the kernel will start the sleep mechanism to enter the sleep!
2. Introduction to the wakelock lock
PowerManager. WakeLockYesLockAndUnlockTwo statuses, and two locks:
①Permanent LockThis lock will not be unlocked unless it is explicitly released, so be very careful when using it!
②Timeout lock, It will be unlocked after the time, and after creating the WakeLock, there areTwo locking mechanisms:
① No-count Lock Mechanism,② Counting lock mechanism (default)
AvailableSetReferenceCounted(Boolean value), the difference is:
RegardlessAcquire() How many times, onceRelease() To unlock the lock.
The latter requires(-Count = 0)When(Count = 0)To apply for a lock.
So,WakeLockThe counting mechanism does not apply for/release a lock for each request;
Only the same lock isApply for/releaseAnd then proceed to the operation!
Ps: the more in-depth content involves the underlying content. The author's level is limited and has not reached that level yet,
I will not study it in depth here. Let's talk about some basic things. If you need them in the future, open 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 (); // in this process, the screen will remain bright! W1.release ();
AboveNewWakeLock() FirstFlagThese labels 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 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
Ps: if you are using a local wake-up lock (UsePARTIAL_WAKE_LOCKMark), the CPU continues to run,
Will ignore any timer, or even press the power button. Other wake-up locks, the CPU will continue to run, but the user still
However, you can press the power button to sleep the device. In addition, you can use more than two tags, but they only affect the behavior of the screen.
AndPARTIAL_WAKE_LOCKIf both are used, there is no impact.
Screen unlock parameters:
ACQUIRE_CAUSES_WAKEUP: Normal wake-up locks do not actually enable illumination. On the contrary, Once opened, they will still
Persistence (for example, the activity of an afterlife user ). When a wakelock is obtained, this flag immediately opens the screen or/and keyboard.
A typical use is to immediately see notifications that are important to users.
ON_AFTER_RELEASE: This flag is set. When wakelock is released, the user activity timer will be reset, resulting in illumination.
For a period of time. If you loop in the wacklock condition, this can be used to reduce the flickering
4. required permissions
To perform power operations, you must declare in AndroidManifest. xml that the application has the permission to set power management:
You may also need:
In addition, the settings of WakeLock are as follows:Activity levelInstead of targeting the entire Application!
Summary:
Okay. This section introduces PowerManager (Power Supply Service), but it is just a matter of time.
Do not use this class unless you have ~ It doesn't matter if you understand it or you don't understand it. You just need to know it!