Android Power Management--Wakelock mechanism

Source: Internet
Author: User

Android Power Management--wakelock mechanism-happy && mundane-blog channel-csdn.net

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 it's not locked or timed out,
The kernel will start the dormant mechanism to go into 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 aPowermanager.wakelock, there are two kinds of mechanisms , the first is not counting the lock mechanism, the other is the counting lock mechanism. Can be done bysetreferencecounted (boolean value)to specify, the general default is the count mechanism. The difference between the two mechanisms is that the former, whether Acquire () How many times, just pass once release () can be unlocked. and The latter is really unlocked in the ( --count
= = 0 ), the same time when (count = = 0) to apply for lock
. SoPowermanager.wakelockThe counting mechanism is not a true sense of the request/release of 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. like Android running up in/sys/power/wake_lock
The following 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
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 to manage
All applications are requested by the Wakelock
. 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 the screen to be displayed 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. And then through PowerManager's newwakelock
      (int flags, String tag) to generate the Wakelock instance. 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 if the application is not released in time, it will cause system malfunction. If you apply for partial wakelock, and do not release in time, the system will never enter sleep mode.

Android Power Management--Wakelock mechanism

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.