博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android PowerManager电源管理
阅读量:4292 次
发布时间:2019-05-27

本文共 3922 字,大约阅读时间需要 13 分钟。

PowerManager

Device battery life will be significantly affected by the use of this API. Do not acquire 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 .

The primary API you'll use is . This will create a  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
On* Off Off
On Dim Off
On Bright Off
On Bright Bright

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

ON_AFTER_RELEASE: 在释放锁时reset activity timer

Public Methods
void (long time)
Forces the device to go to sleep.
boolean ()
Returns whether the screen is currently on.
(int levelAndFlags, tag)
Creates a new wake lock with the specified level and flags.
void ( reason)
Reboot the device.
void (long when, boolean noChangeLights)
Notifies the power manager that user activity happened.
void (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

Public Methods
void ()
Acquires the wake lock.
void (long timeout)
Acquires the wake lock with a timeout.
boolean ()
Returns true if the wake lock has been acquired but not yet released.
void ()
Releases the wake lock.
void (boolean value)
Sets whether this WakeLock is reference counted.
void ( ws)
Sets the work source associated with the wake lock.
()
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);

转载地址:http://gkegi.baihongyu.com/

你可能感兴趣的文章
有序数组求交集
查看>>
文字常量区与栈
查看>>
非阻塞connect 编写方法
查看>>
epoll 边沿触发
查看>>
String类 默认生成的函数
查看>>
Linux 软连接与硬链接
查看>>
视音频数据处理入门:H.264视频码流解析
查看>>
视音频数据处理入门:AAC音频码流解析
查看>>
视音频数据处理入门:UDP-RTP协议解析
查看>>
视音频数据处理入门:FLV封装格式解析
查看>>
最简单的基于FFMPEG的封装格式转换器(无编解码)
查看>>
base64 编码原理
查看>>
单链表是否有环的问题
查看>>
判断两个链表是否相交并找出交点
查看>>
归并排序
查看>>
STL常见问题
查看>>
time_wait和close_wait状态
查看>>
STL中vector、list、deque和map的区别
查看>>
Linux下多线程查看工具(pstree、ps、pstack)
查看>>
PID PPID LWP NLWP
查看>>