Android Power Management--Wakelock mechanism

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/wh_19910525/article/details/8287202

Wake Lock is a mechanism of locking, as long as someone holding this lock, the system will not go into hibernation , can be obtained by the user-state program and the kernel. The lock can be timed out or not timed out, and the timeout lock will automatically unlock after the time has elapsed . If there is no lock or timeout, the kernel will start the dormant mechanism to enter hibernation .

Powermanager.wakelock There are two states of lock and unlock , and there are two ways of locking:

The first kind is permanent lock, such a lock unless explicitly released, otherwise it will not be unlocked, so this kind of lock used to be very careful.

The second type of lock is a time-out lock that is unlocked for a period of time after it has been locked.

when you create a Powermanager.wakelock , there are two kinds of mechanisms , the first is not counting the lock mechanism, the other is the counting lock mechanism. Can be specified by setreferencecounted (boolean value) , which is generally the default count mechanism. The difference between the two mechanisms is that the former no matter how many times acquire () , as long as the first release () Can be unlocked. While the latter is really unlocked when ( --count = = 0 ), the same time (count = = 0) will be applied to lock . So The counting mechanism of the powermanager.wakelock is not a true sense of the request/release each lock, it just the same lock was applied/released the number of times to be counted, and then to operate.

Source Location:frameworks/base/core/java/android/os/powermanager. Java

++++++++++++++++++++++++
tell me how the lock applied by Application layer is uploaded to kernel below to understandthe whole wakelock frame. For example, after Android ran up in the/sys/power/wake_lock below the Powermanagerservicethe build process.
     
1). Application Request a lock
Android provides a ready-made Android.os.PowerManager class, in classprovides the newwakelock(int flags, String tag) method to obtain the application layer The lock , the definition of this function frameworks/base/core/java/android/os/powermanager. Java
The application will call the following section when requesting Wake_lock.
Example:
        powermanager pm = (powermanager) getsystemservice( context.power_service);
Powermanager.wakelock wl = pm. newwakelock(Powermanager.screen_dim_wake_lock, "MyTag");
Wl. acquire ();//apply for lock, this will call Powermanagerservice inside acquirewakelock()

***********************
WL. Release(); Release the lock, display the release lock, and if the requested lock is not released here, the system will not go into hibernation.

  ======================================
2). Frameworks Layer
/frameworks/base/services/java/com/android/server/powermanagerservice. Java This class is the wakelock to manage all applicationapplications. such as the audio-visual, frequency players, camera applications such as Wakelock are managed through this class.
static final String partial_name = "Powermanagerservice"
Nativeacquirewakelock(partial_wake_lock_id, partial_name);
the above function callsPowerThe acquirewakelock ()inside the class, at this timePartial_name is passed as a parameter to the underlying .


Public static native void nativeacquirewakelock(int lock, String ID);
Note: inPowermanagerserviceThe Nativeacquirewakelockis not implemented in the class, and its implementation isin the frameworks/base/core/jni/android_os_power.cpp , theJNI is invoked with the Nativeacquirewakelock () method The implementation method below .



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: The Acquirewakelock () is called in theunder hardware/libhardware_legacy/power/power.c below the paththe acquire_wake_lock(lock, id)




4). Interaction with the kernel layer
The Acquire_wake_lock (lock, id) function under 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 means "/sys/power/wake_lock" The ID is the parameter passed down from the Powermanagerservice class:partial_name = "Powermanagerservice"This is where the file system interacts with the kernel layer.

+++++++++++++++++++++++++++++++++++++++++++++++++++++

the PowerManager class is called by the application to control power device State switching :

The PowerManager class has three external interface functions:

1,void gotosleep(long time);//force device into sleep state

Note: In the application layer call this function, the application needs to compile under the source code, with the system signature, otherwise call this function error;

2,newwakelock(int flags, String tag);//obtain a corresponding level of lock

Flags Parameter Description:

partial_wake_lock : Keep the CPU running, and the screen and keyboard lights are off.

screen_dim_wake_lock : Keep the CPU running, allow screen display but may be gray, turn off the keyboard light

screen_bright_wake_lock : Keep the CPU running, keep the screen highlighted, turn off the keyboard light

full_wake_lock : Keep the CPU running, keep the screen highlighted, and the keyboard lights remain bright

acquire_causes_wakeup: Force open screen and keyboard light once a request lock is in

on_after_release: Reset activity timer When a lock is released

Note:

If you apply for partial wakelock, the system does not enter sleep even when you press the power key, such as when music is playing

If you apply for a different wakelocks, press the Power key and the system will still be in sleep.

3, void useractivity(Long when, Boolean nochangelights),//user activity event occurs, the device will be switched to full in the state, while Reset screen off timer.

procedures for PowerManager and Wakelock
    1. PowerManager pm = (powermanager) getsystemservice(context.power_service); Context.getSystemService() gets the PowerManager instance through the. method.
    2. The Wakelock instance is then generated by PowerManager's newwakelock (int flags, String tag). The INT flags indicates which wakelock to get, and the different lock has different effects on the CPU, screen, keyboard lights.
    3. Gets the Wakelock instance, obtains the corresponding lock through acquire (), and then makes other operations, and finally releases (releases are required) using release () .

Note:

1. in an application that uses the above functions, the following permissions must be included in its manifest.xml file :

<uses-permission android:name="Android.permission.WAKE_LOCK" />

<uses-permission android:name="Android.permission.DEVICE_POWER" />

2.  All locks must be used in pairs and will cause a system failure if the application is not released in a timely manner. If you apply for partial wakelock and do not release it in time, the system will never get into sleep mode.

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.