In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize power management in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve power management in Android"!
Use of the Android power management application layer:
Android provides a ready-made android.os.PowerManager class, which is used to control the switching of the power state of the device.
This class has three interface functions:
Void goToSleep (long time)
/ / force the device to enter the Sleep state
Note:
Try to call the function in the application layer, but failed. The error seems to be insufficient permissions, but it is possible to call it in Service under Framework.
NewWakeLock (int flags, String tag); / / acquire the lock at the corresponding level
Flags parameter description:
PARTIAL_WAKE_LOCK: Screen off, keyboard light off SCREEN_DIM_WAKE_LOCK: screen dim, keyboard light off SCREEN_BRIGHT_WAKE_LOCK: screen bright, keyboard light off FULL_WAKE_LOCK: screen bright, keyboard bright
ACQUIRE_CAUSES_WAKEUP: force Screen and keyboard light to open whenever a lock is requested
ON_AFTER_RELEASE: reset activity timer when releasing the lock
Note:
If you apply for partial wakelock, the system will not enter Sleep even if you press the power key, such as when Music is playing
If you apply for another wakelocks, press the power button, and the system will still enter the Sleep.
Void userActivity (long when, boolean noChangeLights)
/ / User activity event occurs, and the device will be switched to the state of Full on
Meanwhile, Reset Screen off timer.
Sample code:
PowerManager pm = (PowerManager) getSystemService
(Context.POWER_SERVICE)
PowerManager.WakeLock wl = pm.newWakeLock
(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag")
Wl.acquire ()
…… .
Wl.release ()
Note:
1. In an application that uses the above functions, the following permissions must be added to its Manifest.xml file:
< uses-permission android:name= "android.permission.WAKE_LOCK" /> < uses-permission android:name= "android.permission.DEVICE_POWER" />two。 All locks must be used in pairs. If applied and not released in time, it will cause system failure. If you apply for partial wakelock and are not released in time, the system will never enter Sleep mode.
Android Framework level:
The main code file is as follows:
Frameworks\ base\ core\ java\ android\ os\
PowerManager.java
Frameworks\ base\ services\ java\ com\ android\ server\
PowerManagerService.java
Frameworks\ base\ core\ java\ android\ os\ Power.java
Frameworks\ base\ core\ jni\ android_os_power.cpp
Hardware\ libhardware\ power\ power.c
PowerManagerService.java is the core, Power.java provides the underlying function interface to interact with JNI layer, the code of JNI layer is mainly in the file android_os_Power.cpp, the interaction with Linux kernel is realized through Power.c, and the interaction between Android power management and Kernel is mainly realized through sys file, please refer to the introduction of Kernel layer.
The function of this layer is relatively complex, such as the switching of system state, the adjustment and switch of backlight, the application and release of Wake Lock, etc., but this layer has nothing to do with the hardware platform, and Google is responsible for maintenance, there will be relatively fewer problems, interested friends can check the relevant code.
Kernel layer:
The main code is in the following locations:
Drivers/android/power.c
The interface functions provided to Kernel are
EXPORT_SYMBOL (android_init_suspend_lock)
/ / initialize Suspend lock, which must be initialized before use
EXPORT_SYMBOL (android_uninit_suspend_lock)
/ / release resources related to suspend lock
EXPORT_SYMBOL (android_lock_suspend)
/ / apply for lock. You must call the corresponding unlock to release it.
EXPORT_SYMBOL (android_lock_suspend_auto_expire)
/ / apply for partial wakelock. It will be released automatically when the scheduled time expires.
EXPORT_SYMBOL (android_unlock_suspend); / / release lock
EXPORT_SYMBOL (android_power_wakeup); / / Wake up the system to on
EXPORT_SYMBOL (android_register_early_suspend)
/ / Register the driver of early suspend
EXPORT_SYMBOL (android_unregister_early_suspend)
/ / cancel the driver of the registered early suspend
The proc file provided to the Android Framework layer is as follows:
"/ sys/android_power/acquire_partial_wake_lock"
/ / apply for partial wake lock
"/ sys/android_power/acquire_full_wake_lock"
/ / apply for full wake lock
"/ sys/android_power/release_wake_lock"
/ / release the corresponding wake lock
"/ sys/android_power/request_state"
/ / request to change the system state, enter standby and return to wakeup
"/ sys/android_power/state" / / indicates the status of the current system
Android power management is mainly implemented through Wake lock. In the * layer, it is mainly managed through the following three queues:
Static LIST_HEAD (g_inactive_locks); static LIST_HEAD (g_active_partial_wake_locks); static LIST_HEAD (g_active_full_wake_locks)
All initialized lock will be inserted into the g_inactive_locks queue, while the currently active partial wake lock will be inserted into the g_active_partial_wake_locks queue, the active full wake lock will be inserted into the g_active_full_wake_locks queue, and all partial wake lock and full wake lock will be moved to the inactive queue after expiration or unlock, waiting for the next call.
Detailed introduction of Application skills of Android Simulator
Sharing of Android SQLite database application skills
Deep understanding of the way Android Timer is written
Complete solution of Android ListActivity Application skills
Android uses SDK method to explain in detail
The steps to use wake lock in the Kernel layer are as follows:
1. Call the function android_init_suspend_lock to initialize a wake lock
two。 Call the function android_lock_suspend or android_lock_suspend_auto_expire that applies for lock to request lock. You can only apply for partial wake lock here. If you want to apply for Full wake lock, you need to call the function android_lock_partial_suspend_auto_expire (the function does not have an EXPORT). This name is a bit strange, so don't be confused with the previous android_lock_suspend_auto_expire.
3. The wake lock of auto expire can be ignored, otherwise the relevant wake lock must be released in time, otherwise the system will run in a state of high power consumption for a long time.
4. Remember to call android_uninit_suspend_lock to release resources in time when the driver uninstalls or no longer uses Wake lock.
At this point, I believe you have a deeper understanding of "how to achieve power management in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.