Introduction to Android WakeLock

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.