天天看点

android PowerManager电源管理PowerManagerPowerManager.WakeLock

PowerManager

Device battery life will be significantly affected by the use of this API. Do not acquire 

PowerManager.WakeLock

s unless you really need them, use the minimum levels possible, and be sure to release them as soon as possible.

You can obtain an instance of this class by calling 

Context.getSystemService()

.

The primary API you'll use is 

newWakeLock()

. This will create a 

PowerManager.WakeLock

 object. You can then use methods on the wake lock object to control the power state of the device. 

In practice it's quite simple:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
 wl.acquire();
   ..screen will stay on during this section..
 wl.release();
       
Flag Value CPU Screen Keyboard

PARTIAL_WAKE_LOCK

On* Off Off

SCREEN_DIM_WAKE_LOCK

On Dim Off

SCREEN_BRIGHT_WAKE_LOCK

On Bright Off

FULL_WAKE_LOCK

On Bright Bright

ACQUIRE_CAUSES_WAKEUP: 一旦有请求锁时强制打开Screen和keyboard light

ON_AFTER_RELEASE: 在释放锁时reset activity timer

Public Methods
void goToSleep(long time) Forces the device to go to sleep.
boolean isScreenOn() Returns whether the screen is currently on.
PowerManager.WakeLock newWakeLock(int levelAndFlags, String tag) Creates a new wake lock with the specified level and flags.
void reboot( String reason) Reboot the device.
void userActivity(long when, boolean noChangeLights) Notifies the power manager that user activity happened.
void wakeUp(long time) Forces the device to wake up from sleep.

PowerManager.WakeLock

A wake lock is a mechanism to indicate that your application needs to have the device stay on.

Any application using a WakeLock must request the

android.permission.WAKE_LOCK

permission in an

<uses-permission>

element of the application's manifest. Obtain a wake lock by calling

newWakeLock(int, String)

Public Methods
void acquire() Acquires the wake lock.
void acquire(long timeout) Acquires the wake lock with a timeout.
boolean isHeld() Returns true if the wake lock has been acquired but not yet released.
void release() Releases the wake lock.
void setReferenceCounted(boolean value) Sets whether this WakeLock is reference counted.
void setWorkSource( WorkSource ws) Sets the work source associated with the wake lock.
String toString() Returns a string containing a concise, human-readable description of this object.

***************************************************************--------------*******************************************************

接下来我们从Java应用层面, Android framework层面, Linux内核层面分别进行详细的讨论:

应用层的使用:

Android提供了现成android.os.PowerManager类,该类用于控制设备的电源状态的切换.

Android Framework层面:

其主要代码文件如下:

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是核心, Power.java提供底层的函数接口,与JNI层进行交互, JNI层的代码主要在文件android_os_Power.cpp中,与Linux kernel交互是通过Power.c来实现的, Andriod跟Kernel的交互主要是通过sys文件的方式来实现的,具体请参考Kernel层的介绍.

这一层的功能相对比较复杂,比如系统状态的切换,背光的调节及开关,Wake Lock的申请和释放等等,但这一层跟硬件平台无关,而且由Google负责维护,问题相对会少一些,有兴趣的朋友可以自己查看相关的代码.

Kernel层:

其主要代码在下列位置:

drivers/android/power.c

其对Kernel提供的接口函数有

EXPORT_SYMBOL(android_init_suspend_lock); //初始化Suspend lock,在使用前必须做初始化

EXPORT_SYMBOL(android_uninit_suspend_lock); //释放suspend lock相关的资源

EXPORT_SYMBOL(android_lock_suspend); //申请lock,必须调用相应的unlock来释放它

EXPORT_SYMBOL(android_lock_suspend_auto_expire);//申请partial wakelock, 定时时间到后会自动释放

EXPORT_SYMBOL(android_unlock_suspend); //释放lock

EXPORT_SYMBOL(android_power_wakeup); //唤醒系统到on

EXPORT_SYMBOL(android_register_early_suspend); //注册early suspend的驱动

EXPORT_SYMBOL(android_unregister_early_suspend); //取消已经注册的early suspend的驱动

提供给Android Framework层的proc文件如下:

"/sys/android_power/acquire_partial_wake_lock" //申请partial wake lock

"/sys/android_power/acquire_full_wake_lock" //申请full wake lock

"/sys/android_power/release_wake_lock" //释放相应的wake lock

"/sys/android_power/request_state" //请求改变系统状态,进standby和回到wakeup两种状态

"/sys/android_power/state" //指示当前系统的状态

Android的电源管理主要是通过Wake lock来实现的,在最底层主要是通过如下三个队列来实现其管理:

static LIST_HEAD(g_inactive_locks);

static LIST_HEAD(g_active_partial_wake_locks);

static LIST_HEAD(g_active_full_wake_locks);