zoukankan      html  css  js  c++  java
  • PowerManager简介

    文章参照: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
  • 相关阅读:
    Python使用shape计算矩阵的行和列
    python--tile函数
    【Machine Learning in Action --3】决策树ID3算法
    python [1:3]
    python字典访问的三种方法
    python--sorted函数和operator.itemgetter函数
    python--lambda和def函数
    python--sorted函数
    【转载】梦断计院--一个计算机学院学生大学学习生活的回顾与反省
    jquery源码学习-初始(1)
  • 原文地址:https://www.cnblogs.com/liyuzhao/p/3818169.html
Copyright © 2011-2022 走看看