PowerManager和PowerManager.WakeLock详解
Timer项目。介绍一下PowerManager和PowerManager.WakeLock。
PowerManager.WakeLock
- 代码
- 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;
- }
- }
- @Override
- protectedvoid onResume()
- {
- super.onResume();
- //获取锁,保持屏幕亮度
- acquireWakeLock();
- }
- 代码
- protectedvoid onPause()
- {
- super.onPause();
- synchronized(this) {
- releaseWakeLock();
- if (finished) {
- clearState();
- } else {
- saveState();
- }
- }
- }
- 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
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:if 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.
权限获取
- <uses-permission android:name="android.permission.WAKE_LOCK"/>
- 可能还需要
- <uses-permission android:name="android.permission.DEVICE_POWER"/>
另外,WakeLock的设置是 Activiy 级别的,也可以其他组件中使用,只要在acquire和release之间,执行完代码就可以了。不是针对整个Application应用的。比如我做的项目中的代码分享出来一段。
PowerManager.WakeLock mwl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
mwl .acquire();
Intent serviceIntent = new Intent(context, LocalPushService.class);
serviceIntent.putExtra(Constants.EXTRA_HOT_TOPIC_ALARM, true);
context.startService(serviceIntent);
mwl .release();
PowerManager和PowerManager.WakeLock详解的更多相关文章
- Android -- PowerManager和PowerManager.WakeLock
PowerManager.WakeLock PowerManager.WakerLock是我 ...
- Wakelock API详解
官方资料 http://developer.android.com/intl/zh-CN/reference/android/os/PowerManager.WakeLock.html http:/ ...
- Android核心分析之十五Android输入系统之输入路径详解
Android用户事件输入路径 1 输入路径的一般原理 按键,鼠标消息从收集到最终将发送到焦点窗口,要经历怎样的路径,是Android GWES设计方案中需要详细考虑的问题.按键,鼠标等用户消息 ...
- android中getSystemService详解
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...
- Android 多线程之IntentService 完全详解
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
- adb shell命令模拟按键/输入input使用keycode 列表详解
在adb shell里有一个非常使用的命令,模拟按键输入,这里首先不要理解为是键盘的模拟按键,下面命令的使用和键值做一个详解. input命令格式 adb shell input keyevent & ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
随机推荐
- 使用locate 的正则查询 查找所有main.c
locate支持正则查询的功能, 只需输入locate -r 正则表达式 即可. 现在我想查找所有main.c怎么做? 打开终端,输入shell: locate -r main.c$ PS:' ...
- 生成csv格式文件并导出至页面的前后台实现
一.前台实现: 1. HTML: <div> <a href="javascript:void(0);" class="btnStyleLeft&quo ...
- MySQL死锁案例分析与解决方案
MySQL死锁案例分析与解决方案 现象: 数据库查询: SQL语句分析: mysql. 并发delete同一行记录,偶发死锁. delete from x_table where id=? ...
- Django之反向生成url
首先新建一个项目test_url,项目包含一个名为app01的应用 在urls.py文件中生成如下内容 from django.conf.urls import url from django.sho ...
- Spring MVC Mock demo
package com.niwodai.mem.web.controller; import com.alibaba.fastjson.JSON; import org.junit.Before; i ...
- MySQL计划任务(事件调度器)(Event Scheduler)[转]
原文链接: http://www.cnblogs.com/c840136/articles/2388512.html MySQL5.1.x版本中引入了一项新特性EVENT,顾名思义就是事件.定时任务机 ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- 创建hbase-indexer出现 0 running
新建hbase-indexer后通过hbase-indexer list-indexers发现SEP subscription ID: null并且0 running processes,如下: IN ...
- Spring学习——从入门到精通
本文章是博主原创,转载需注明出处. 第一篇先简单入个门--通过Spring创建对象 开发环境为Myeclipse2013,JDK版本为1.6,不要嫌它老,新知识都是在旧知识的基础上建立起来的,所谓基础 ...
- Redis 实践2-数据结构
alias redis-cli='/usr/local/redis/bin/redis-cli' vi .bashrc 编辑加入 alias redis-cli='/usr/local/redi ...