Introduction to Android WakeLock
Wake Lock is a Lock mechanism. As long as someone holds this Lock, the system cannot enter sleep,
The lock can be obtained by user State programs and kernels. The lock can be time-out or no time-out,
The expired lock will be automatically unlocked after the time passes. If no lock is available or the lock times out, the kernel will
The sleep mechanism is enabled to enter sleep.
(1). kernel maintenance:
1). Two linked lists, active_wake_locks [WAKE_LOCK_TYPE_COUNT]
Active_wake_locks [0] maintains the suspend lock.
Active_wake_locks [1] maintains the idle lock.
2). A linked list, inactive_locks, to record all locks in inactive state.
(2) The following describes how to transfer the lock applied by the application layer to the kernel to understand
The entire wakelock framework.
For example, PowerManagerService under/sys/power/wake_lock
.
1). Android provides the ready-made android. OS. PowerManager class, in which
Provides the newWakeLock (int flags, String tag) method to obtain the corresponding
Hierarchical lock, defined by this function
Frameworks/base/core/java/android/OS/PowerManager. java
Next, the application will call wae_lock.
Instance:
PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );
PowerManager. WakeLock wl = pm. newWakeLock
(PowerManager. SCREEN_DIM_WAKE_LOCK, "My Tag ");
Wl. acquire (); // apply for a lock. This will call acquireWakeLock () in PowerManagerService ()
***********************
Wl. release (); // release lock, displayed release. If the applied lock is not released here, the system will not enter sleep.
2). frameworks Layer
/Frameworks/base/services/java/com/android/server/
PowerManagerService. java
This class is used to manage the wakelock applied by all applications. For example, audio and video
Frequency player, camera, and other applications for wakelock are managed through this class.
Static final String PARTIAL_NAME = "PowerManagerService"
Power. acquireWakeLock (Power. PARTIAL_WAKE_LOCK,
PARTIAL_NAME );
This function calls acquireWakeLock () in the Power class.
PARTIAL_NAME is passed as a parameter to the underlying layer.
/Frameworks/base/core/java/android/OS/Power. java
Public static native void acquireWakeLock (int lock, String id );
Note: native declarative methods are not implemented in the Power class, and their implementations are
In frameworks/base/core/jni/android_ OS _Power.cpp
To call the acquireWakeLock () method of the Power class, JNI is called.
.
3) implementation of the jni Layer
Path: frameworks/base/core/jni/android_ OS _Power.cpp
Static void acquireWakeLock (JNIEnv * env, jobject clazz,
Jint lock, jstring idObj)
{
**************
Const char * id = env-> GetStringUTFChars (idObj, NULL );
Acquire_wake_lock (lock, id );
Env-> ReleaseStringUTFChars (idObj, id );
}
Note: It is called in acquireWakeLock ()
Path: hardware/libhardware_legacy/power. c
Acquire_wake_lock (lock, id))
4). Interaction with the kernel layer
The acquire_wake_lock (lock, id) function in power. c is as follows:
Int acquire_wake_lock (int lock, const char * id)
{
**************
Return write (fd, id, strlen (id ));
}
Note: fd is the file descriptor, which indicates "/sys/power/wake_lock"
Id is the parameter passed in from the PowerManagerService class:
PARTIAL_NAME = "PowerManagerService"
This is where the file system interacts with the kernel layer.