PowerManager简介

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                文章参照:http://developer.android.com/reference/android/os/PowerManager.html#userActivity%28long,%20boolean%29
android.os. PowerManager
通过 PowerManager 类我们可以对设备的电源进行管理。对该类API的使用将影响到电池寿命。只有在必须使用WakeLocks的时候,才使用WakeLocks,且在不使用它的时候要及时释放(release).
图一 :
PowerManager - hubingforever - 民主与科学
 
默认情况下,当用户对手机有一段时间没有操作后,手机的Keyboard(这里不仅仅指硬键盘,还包括其他的所有键,比如Menu)背光将消失,从Bright变为Off,如果再过段时间没操作,屏幕(Screen)将从高亮(Bright)变为暗淡(Dim),如果再过段时间没操作,屏幕(Screen)将又由暗淡(Dim)变为不显示(Off),如果再过段时间没操作,CPU将sleep,从on变为off.通过PowerManager类可以对上述过程进行管理,可以让设备到达上面的某种状态时,该状态将不再超时,将不再往下走,但是仍然可以跳到到更上级的某种状态(比如用户有活动,可以让手机回到最高状态)。
你可以通过 Context.getSystemService()方法来得到 PowerManager类的实例。你通常需要使用的是 newWakeLock(),它将创建一个 PowerManager.WakeLock实例。你可以通过该对象的方法来对电源进行管理。
比如, 示例1:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
 wl.acquire();
   ..screen will stay on during this section..
 wl.release();
你可以如下的flag,来说明将进行怎样的电源管理。下面的flag都是互斥,你只有使用其中的一个。
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
如果你hold了一个 partial wakelock ,那么 CPU将一直运行 , 甚至在用户按下电源按钮 。 对于 其他的wakelocks ,那么 CPU将继续运行 ,但是用户可以 通过按下电源按钮来停止CPU的运行 。我们可以创建多个锁,并hold它,即使对同一类型,也如此,对于某类型的wakelock只要有一个被hold,那么它所对应的电源状态(illumination),就将不会超时,将被延续(hold).在上表中我们把越往下的,称为更高一级的wakelocks.当高级和低级wakelocks相遇的时候,高级起作用。
在上面的flag上还再加上如下的2个flag,但是他们和PARTIAL_WAKE_LOCK.组合没任何意义
ACQUIRE_CAUSES_WAKEUP
默认情况下wake locks并不是马上开启CPU或Screen或Keyboard的illumination(对于Screen是Dim或Bright,Keyboard是Bright. wake locks只是在被开启后(比如用户的活动),让设备延续(保存)你设定开启的状态. 但是如果加上ACQUIRE_CAUSES_WAKEUP就可以让Screen或Keyboar的illumination没开启的情况,马上开启它们。 典型的应用就是在收到一个重要的notifications时,需要马上点亮屏幕。
ON_AFTER_RELEASE
如果有该flag, 那么在WakeLock被释放的时候,user activity计时器将被重设, 这样illumination将持续一段更长的时间.This can be used to reduce flicker if you are cycling between wake lock conditions.
主要函数:
public void goToSleep (long time)
Since: API Level 1
Force the device to go to sleep. Overrides all the wake locks that are held.
参数
time  is used to order this correctly with the wake lock calls. The time should be in the SystemClock.uptimeMillis() time base.
该函数用于强制让设备进入休眠状态。
注意:在Eclipse环境中我使用该函数运行失败.可能需要在内核编译的环境中使用才行。
public boolean isScreenOn ()
Since: API Level 7
Returns whether the screen is currently on. The screen could be bright or dim.
用于判断屏幕是否处于点亮状态(包括Bright和dim)
示例2:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 boolean isScreenOn = pm.isScreenOn();
public PowerManager.WakeLock newWakeLock (int flags, String tag)
Since: API Level 1
创建一个flag所指定的类型的wake lock对象,可以通过调用该对象的acquire()方法在获得一个wake锁, 使用完后可以通过release() 释放该锁
示例3:
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,
                                      TAG);
wl.acquire();
 // ...
wl.release();
参数
flags  Combination of flag values defining the requested behavior of the WakeLock.
用于指定创建的wake lock对象的类型
tag  Your class name (or other tag) for debugging purposes.用于标记是在哪个地方创建的该wake lock对象,以便调试用。
public void reboot (String reason)
Since: API Level 8
Reboot the device. Will not return if the reboot is successful. Requires the REBOOT permission.
参数
reason  code to pass to the kernel (e.g., "recovery") to request special boot modes, or null.
该函数用于重启设备。
注意:在Eclipse环境中我使用该函数运行失败.可能需要在内核编译的环境中使用才行。
public void userActivity (long when, boolean noChangeLights)
Since: API Level 1
User activity happened.
Turns the device from whatever state it's in to full on, and resets the auto-off timer.
参数
when  is used to order this correctly with the wake lock calls. This time should be in the SystemClock.uptimeMillis() time base.
noChangeLights  should be true if you don't want the lights to turn on because of this event. This is set when the power key goes down. We want the device to stay on while the button is down, but we're about to turn off. Otherwise the lights flash on and then off and it looks weird.
该函数主要就是用于通知系统有个user Activity发生了 ,when就是指user Activity发生的时间,该时间是基于 SystemClock.uptimeMillis() ,noChangeLights是指是否需要因为该user Activity而把Screen和Keyboard的Lights点亮。当我们按下power键要进行关屏的时候,就不需要点亮Screen和Keyboard的Lights,所以该参数为true,否则的话,先点亮Screen和Keyboard的Lights,然后再关掉屏幕,就很奇怪了。
需要的permission
需要在AndroidManifest.xml中加入以下2个permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DEVICE_POWER"/>
最后,关于 PowerManager的练习可以参照 《PowerManager使用实例1
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值