PowerManager和PowerManager.WakeLock的用法

本文参考: android的PowerManager和PowerManager.WakeLock


PowerManager:This class gives you control of the power state of the device.
PowerManager.WakeLock: A wake lock is a mechanism to indicate that your application needs to have the device stay on
Android中通过各种Lock锁对电源进行控制,需要注意的是通常情况下加锁和解锁必须成对出现(为什么说通常是成对存在的,文章结尾说明)。先上一段代码然后进行说明。
复制代码
代码
private void  acquireWakeLock() {
if  (wakeLock  == null ) {
Logger.d(
" Acquiring wake lock " );
PowerManager pm 
=  (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock 
=  pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,  this .getClass().getCanonicalName());
wakeLock.acquire();
}

}


private void  releaseWakeLock() {
if  (wakeLock  != null &&  wakeLock.isHeld()) {
wakeLock.release();
wakeLock 
= null ;
}

}
复制代码

 

acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。
复制代码
@Override
protected void  onResume()
{
super .onResume();
// 获取锁,保持屏幕亮度
acquireWakeLock();
startTimer();
}
复制代码
代码

 

PowerManager和WakeLock的操作步骤
  1.   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。
  2.   然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
  3.   获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。

关于int flags

各种锁的类型对CPU 、屏幕、键盘的影响:

PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。

SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯

SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯

FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度

ACQUIRE_CAUSES_WAKEUP:Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.

ON_AFTER_RELEASE:f this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

权限获取

要进行电源的操作需要在AndroidManifest.xml中声明该应用有设置电源管理的权限。
< uses-permission  android:name ="android.permission.WAKE_LOCK" />
你可能还需要
< uses-permission  android:name ="android.permission.DEVICE_POWER" />
 
注意:WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。

PowerManager.WakeLock 有一个方法 setReferenceCounted(boolean value): Sets whether this WakeLock is reference counted.  Wake locks are reference counted by default. If a wake lock is reference counted, then each call to acquire() must be balanced by an equal number of calls to release(). If a wake lock is not reference counted, then one call to release() is sufficient to undo the effect of all previous calls to acquire().   
官方API上说的挺清楚了,如果调用了该方法setReferenceCounted(false),那么只需要调用一次release()方法就可以释放所有之前获得的WakeLock。默认情况是setReferenceCounted(true) ,记获取锁与释放锁的数量要对应。   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值