Android WakeLock电源管理
2013-09-28 15:11
393 查看
前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
PowerManager:This class gives you control of the power state of the device.
PowerManager.WakeLock: lets you say that you need to have the device on.
Android中通过各种Lock锁对电源进行控制,需要注意的是加锁和解锁必须成对出现。先上一段Standup Timer里的代码然后进行说明。
acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。
PowerManager和WakeLock的操作步骤
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过
然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int
Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。
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.
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
PowerManager.WakeLock
PowerManager.WakerLock是我分析Standup Timer源代码时发现的一个小知识点,Standup Timer 用WakeLock保证程序运行时保持手机屏幕的恒亮(程序虽小但也做得相当的细心,考虑的很周到)。PowerManager 和PowerManager.WakerLock7用于对Android设备的电源进行管理。PowerManager:This class gives you control of the power state of the device.
PowerManager.WakeLock: lets you say that you need to have the device on.
Android中通过各种Lock锁对电源进行控制,需要注意的是加锁和解锁必须成对出现。先上一段Standup Timer里的代码然后进行说明。
代码 privatevoid 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(); } } privatevoid releaseWakeLock() { if (wakeLock !=null&& wakeLock.isHeld()) { wakeLock.release(); wakeLock =null; } }
acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。
@Override protectedvoid onResume() { super.onResume(); //获取锁,保持屏幕亮度 acquireWakeLock(); startTimer(); }
代码 protectedvoid onPause() { super.onPause(); synchronized(this) { cancelTimer(); releaseWakeLock(); if (finished) { clearState(); } else { saveState(); } } }
PowerManager和WakeLock的操作步骤
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过
Context.getSystemService().方法获取PowerManager实例。
然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int
Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
获取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"/>
相关文章推荐
- Android图形系统的底层实现
- 在PC上体验Android(转)
- Android提高第十一篇之模拟信号示波器
- CSDN移动博文集锦之Android游戏开发
- 将android launcher2导入eclipse中运行
- Android启动分析
- Android Ap 开发 设计模式第九篇:桥接模式 推荐
- android avd无法启动问题
- Android 上层RecoverySystem类
- 才学 android 做了几个小例子 进度条
- android之Uri的常用几个例子
- [Android]android源码下载&Eclipse关联android源码
- Android Bitmap和Canvas学习笔记
- 在Android中调用WebService
- Android 设置wap请求方式
- Android进阶2之APK方式换肤
- Android系统Recovery工作原理之使用update.zip升级过程分析(四)---Android系统Recovery模式的工作原理
- android Fragments详解二:创建Fragment
- Android中从SD卡中/拍照选择图片并进行剪裁的方法
- 转战Android之NDK(图形绘制)