博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PowerManager和WakeLock的操作步骤
阅读量:6708 次
发布时间:2019-06-25

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

①  PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 .方法获取PowerManager实例。

②然后通过PowerManager的newWakeLock((int flags,  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" />

另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。

 

 

可以在activity的onResume方法里面操作WakeLock,  在onPause方法里面释放。

 

转载于:https://www.cnblogs.com/china-soft/p/3440671.html

你可能感兴趣的文章
arduino 串口读取字符串_Arduino传感器教程 第24章NRF24L01 控制电舵机
查看>>
状态码202_HTTP状态码(HTTP Status Code)
查看>>
sharepoint 2010 网站集定期备份
查看>>
管理SCCM/MDT中的驱动分类
查看>>
java之HashTable
查看>>
Windows Server 2012体验之配置存储池
查看>>
轻松上手移动互联——百度SiteApp建造日志
查看>>
我从跑步中领悟到了什么?
查看>>
你的权限等于你的可见度
查看>>
Gartner:威胁情报的定义
查看>>
redis多实例重启脚本
查看>>
开发人员学Linux(4):使用JMeter对网站和数据库进行压力测试
查看>>
在51系列中data,idata,xdata,pdata的区别
查看>>
【Deeplearning】关注书目
查看>>
【再见RMQ】NYOJ-119-士兵杀敌(三),区间内大小差值
查看>>
loadrunner中Run-time-Setting设置
查看>>
SSL连接建立过程分析(1)
查看>>
port与大全portClose方法
查看>>
美丽的数学家:如果您讨厌数学,这些其实都是人生故事
查看>>
Kettle 中转换(transformation)的执行过程
查看>>