赞
踩
Android系统提供了电源管理的类PowerManager;调用相应的属性即可实现;
系统服务 --- PowerManager.java
顾名思义,PowerManager.java就是管理我们电源方面的功能的,当然也包括我们屏幕的亮和熄灭。没错,我们的应用程序就是通过这个系统服务来实现亮屏和熄屏的功能的。类型 | cpu | 屏幕 | 键盘 |
PARTIAL_WAKE_LOCK | on | off | off |
SCREEN_DIM_WAKE_LOCK | on | dim | off |
SCREEN_BRIGHT_WAKE_LOCK | on | Bright | off |
FULL_WAKE_LOCK | on | Bright | Bright |
那么,这里要注意的是acquire()和release()要成对调用哦!也就是说,你申请了亮屏,过段时间,你要是释放它。
PowerManager这个类提供了电源管理的一些功能,比如可以让屏幕或者键盘亮起来等。还有对设备的重启的api
官网是这么解释的
这个类提供了控制设备电源状态的管理功能。
设备的电池的持续时间(寿命)会受到使用这个API的重要影响。在非必要的情况下不要使用WakeLock,即使必须使用,也要最低限度使用这个api,使用完之后应确保立即释放掉。
通过 Context.getSystemService()获得PowerManager的实例。
使用PowerManager的实例去获得一个WakeLock对象,使用这个方法:newWakeLock() 创建一个
PowerManager.WakeLock
对象.使用WakeLock对象可以去管理设备电源的状态,使用方法特别简单:如下例子
<span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">PowerManager</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> pm </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">=</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">)</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> getSystemService</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">Context</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);">POWER_SERVICE</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">);</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> </span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">WakeLock</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> wl </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">=</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> pm</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);">newWakeLock</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);">SCREEN_DIM_WAKE_LOCK</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">,</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> </span><span class="str" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 136, 0);">"My Tag"</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">);</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> wl</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);">acquire</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">();</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> //在释放之前,屏幕一直亮着(有可能会变暗,但是还可以看到屏幕内容)</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> wl</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);">release</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(102, 102, 0);">();</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.2000007629395px; color: rgb(0, 0, 0);"> </span>
下面定义的这些标记不同程度的影响系统电源。这些标记都是独占的,并且每次只能指定其中一个。
Flag Value | CPU | Screen | Keyboard |
---|---|---|---|
PARTIAL_WAKE_LOCK | On* | Off(关闭) | Off(关闭) |
SCREEN_DIM_WAKE_LOCK | On | Dim(变暗) | Off(关闭) |
SCREEN_BRIGHT_WAKE_LOCK | On | Bright(高亮) | Off(关闭) |
FULL_WAKE_LOCK | On | Bright(高亮) | Bright(高亮) |
如果你使用的是局部唤醒锁的话(使用PARTIAL_WAKE_LOCK标志),CPU会继续运行,将忽略任何的计时器,甚至按下电源按钮。其他的唤醒锁话,CPU也会继续运转,但是使用者仍然可以按电源按钮让设备睡眠。
另外,你可以使用两个以上的标记,但是他只影响屏幕的行为。和 PARTIAL_WAKE_LOCK 同时使用的话,没有任何影响。
Flag Value | Description |
---|---|
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.正常情况下是不会是屏幕等变亮,相反,当获得wakeLock之后需要一个触发事件才会使屏幕或者键盘变亮。典型应用是一个对用户来说比较重要的通知时,使用这个锁。 |
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.帮忙翻译一下吧 |
一个小例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<strong>
package
com.example.powers;
import
android.os.Bundle;
import
android.os.PowerManager;
import
android.os.PowerManager.WakeLock;
import
android.app.Activity;
import
android.content.Context;
import
android.util.Log;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.support.v4.app.NavUtils;
public
class
MainActivity
extends
Activity {
private
WakeLock wakeLock;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected
void
onResume() {
super
.onResume();
// 开始获得唤醒锁
acquireWakeLock();
}
@Override
public
void
finish() {
super
.finish();
// 释放锁
releaseWakeLock();
}
private
void
acquireWakeLock() {
if
(wakeLock ==
null
) {
Log.d(
"debug"
,
"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
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}</strong>
|
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。