- 浏览: 624977 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
luo_ganlin:
别的不多说,点个赞!
关于Android隐式启动Activity -
IWSo:
谢楼主!研究了好久,原来是这样!
android中如何让LinearLayout实现点击时背景图片切换 -
fantao005x:
粘帖的不错
android中如何让listview的内容全部显示出来 -
learner576539763:
Android_gqs 写道请问博主,Viewstub 可实现 ...
android中ViewStub使用 -
goontosoon:
抄的什么啊,狗屁不通
对ContentProvider中getType(Uri uri)
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中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。
protected void onResume()
{
super.onResume();
//获取锁,保持屏幕亮度
acquireWakeLock();
startTimer();
}
- 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: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.
权限获取
你可能还需要
<uses-permission android:name="android.permission.DEVICE_POWER" />
*如果你获得一个不完整的wakelock,则CPU会继续运行,不必顾及任何计时器甚至用户按下power键后。对于所有的其他的wakelock,CPU在运行,但用户可以直接用power键使设备休眠。
*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.
另外,你能加两个以上的标志,这些仅能影响屏幕的行为。这些标志当组合中有一个PARTIAL_WAKE_LOCK时将没有效果。
In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a PARTIAL_WAKE_LOCK
.
正常的wake lock不会实际转到照明。反而,他们使照明保持自他打开时(例如,来自用户activity)。这个标志会强制屏幕和/或键盘立即打开,当这个wakelock已获取时。一个典型用法是将哪些重要的通知立即让用户看到。 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. |
如果这个标志被设置,用户activity计时器当WakeLock被释放时会重新设定,因为照明保持一点长。这个可以用于降低闪烁如果你是在wake lock状态下循环。 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. |
Summary
class |
PowerManager.WakeLock |
类让你说你需要有这个设备。 Class lets you say that you need to have the device on. |
int |
ACQUIRE_CAUSES_WAKEUP |
正常的wake lock不会实际的唤醒设备,他们自他准备好则保持着。 Normally wake locks don't actually wake the device, they just cause it to remain on once it's already on. |
|
int |
FULL_WAKE_LOCK |
Wake lock 以确保屏幕和键盘全部点亮。 Wake lock that ensures that the screen and keyboard are on at full brightness. |
|
int |
ON_AFTER_RELEASE |
当这个wake lock已释放时,拨开了用户界面的计时器所以屏幕停留稍微长。 When this wake lock is released, poke the user activity timer so the screen stays on for a little longer. |
|
int |
PARTIAL_WAKE_LOCK |
Wake lock以确保CPU运行。 Wake lock that ensures that the CPU is running. |
|
int |
SCREEN_BRIGHT_WAKE_LOCK |
Wake lock以确保屏幕全亮;键盘背光允许关闭。 Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off. |
|
int |
SCREEN_DIM_WAKE_LOCK |
Wake lock以确保屏幕打开(但可以是暗淡的);键盘背光将允许关闭。 Wake lock that ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off. |
void |
goToSleep(long time) 强制设备进入休眠。 Force the device to go to sleep. |
|
boolean |
IsScreenOn() 返回屏幕是否当前亮着。 Returns whether the screen is currently on. |
|
PowerManager.WakeLock |
newWakeLock(int flags, String tag) 得到一个Wake lock在这个标志参数的级别。 Get a wake lock at the level of the flags parameter. |
|
void |
reboot(String reason) 重启设备 Reboot the device. |
|
void |
userActivity(long when, boolean noChangeLights) 用户界面发生。 User activity happened. |
发表评论
-
EditText软键盘弹出问题解决
2013-02-26 23:10 1408当带有EditView的activity第一次进入时,第一 ... -
android中获取系统相关属性adb
2012-11-15 14:41 21521.查看系统相关属性可以通过: adb shell ... -
Android使用Intent传递复杂参数及复杂参数列表
2012-11-05 17:29 1581刚开始一直纠结于Intent只能put像int, ... -
解决P6200/P6800扩展卡第三方软件不可写的BUG
2012-11-05 17:01 988从XDA看来的步骤:1. Using a root-e ... -
android 中跟actionbar相关的属性
2012-10-25 17:07 2436android:uiOptions 这个属性用于设置A ... -
source insight使用快捷键
2012-10-25 10:59 1508F5指定行号,实现行跳转,在遇到编译错误的时候,能特 ... -
android中推出应用比较有效率的方法
2012-10-11 16:57 1125添加一个全局变量作为程序退出的标记(boolean类型) ... -
declare-styleable的使用
2012-10-09 13:59 1121declare-styleable的使用 decl ... -
android程序安全的建议
2012-09-29 14:58 5217如果保证自己的 ... -
Java自带的线程池ThreadPoolExecutor详细介绍说明和实例应用
2012-09-29 14:45 1036从 Java 5 开始,Java 提供了自己的线程池。线 ... -
android应用检测更新代码
2012-09-24 17:40 1754JAVA代码: UpdateManager.java ... -
adb命令详解
2012-09-19 15:04 2787Android adb的常用命令略解 Androi ... -
android中屏蔽其它系统按钮的dialog
2012-09-18 10:13 1611public class MyProgress ... -
如何给Scrollview里内容截屏并生成bitmap,注意:Scrollview里面内容较多有滚动了
2012-09-18 10:07 1603使用for循环递归累加其内部的子控件的高度: p ... -
启动另外一个apk
2012-09-14 13:16 860这篇博文主要是获取其他apk程序的启动的主intent, ... -
android中全屏的方法
2012-09-14 13:04 9281.直接代码编写 @Override ... -
android:installLocation简析
2012-09-12 15:25 1073在Froyo(android 2.2,API Le ... -
外部apk启动启动另外一个apk
2012-09-06 17:54 1011public class TestingBroadc ... -
listview如何实现圆角
2012-09-05 17:32 1902首先呢,我们还是看几个示图:(这是360推出的一款天气预 ... -
android中如何更有效率得解析xml
2012-09-05 17:17 1277好久没写过博文了,最近在做xml方面的解析,x ...
相关推荐
WakeLock使用方法代码实例,需要的朋友可以参考一下
获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他操作,最后使用release()释放(释放是必须的)。 Note: 1. 在使用以上函数的应用程序中,必须在其Manifest.xml文件中加入下面的权限: ...
wakelock插件使用。 对于此插件,这意味着基本API是使用定义的。 鸽子文件可在主软件包的中找到。 该API是在Dart中的定义的。 此外,Android和iOS实现可在主程序包中找到,而Web实现则在。 此仓库中的软件包如下: ...
关于android节能,可以利用和提高的方面 适合小论文
Hold a wakelock that can be acquired in the AlarmReceiver and released in the AlarmAlert activity for Andriod.
浅析Wakelock机制与Android电源管理.pdf
本篇文章主要介绍了使用WakeLock使Android应用程序保持后台唤醒的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
各种锁的类型对CPU 、屏幕、键盘的影响:SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯SCREEN
我们可以通过代理对应的 Service 实现,完成收集 Wakelock、Alarm、GPS 的申请堆栈、释放信息、手机充电状态等等。
:house: :sparkles:浏览器支持 react-screen-wake-lock使用原生的Screen Wake Lock API,并非所有浏览器都支持。 安装npm i react-screen-wake-lock# oryarn add react-screen-wake-lock用法 import { useWakeLock ...
step_counter_with_wakeLock 尝试让唤醒锁保持 step_counter 唤醒的测试,因为它实际上在屏幕关闭后立即关闭。 我已经修改了的代码,以检查 WakeLock 是否对我的三星 S4 有帮助:没有任何人可以帮忙吗?
屏幕唤醒锁定API 您可以在查看完整的文档。
这是Wake Lock API的包装。 安装 npm install @alxshelepenok/awaker 执照 麻省理工学院执照 版权所有(c)2021 Alexander Shelepenok 特此免费授予获得该软件和相关文档文件(“软件”)副本的任何人无限制使用...
一个基于Geolocation API,AmbientLightSensor API和WakeLock API的小巧,简洁的速度计渐进式Web应用程序。 为什么? 因为我的2005 Prius上的组合仪表处于故障状态,所以我需要一个测速仪,直到可以拆开并修理有...
唤醒锁资源管理初始提交自定义 Android 电源管理框架,以便在管理各种 Android 应用程序获取的唤醒锁时提供更多用户级别的控制。 这可以使高级用户能够更有效地管理 Android 手机上的电池使用情况
React本机接近唤醒锁React本机接近唤醒锁(仅适用于Android)