PowerManager和PowerManager.WakeLock

android的PowerManager和PowerManager.WakeLock

转载自:http://www.cnblogs.com/keyindex/articles/1819504.html

PowerManager.WakeLock
  PowerManager.WakerLock是我分析Standup Timer源代码时发现的一个小知识点,Standup Timer 用WakeLock保证程序运行时保持手机屏幕的恒亮(程序虽小但也做得相当的细心,考虑的很周到)。PowerManager 和PowerManager.WakerLock7用于对Android设备的电源进行管理。
  PowerManager:This class gives you control of the power state of the device.
  PowerManager.WakeLock: lets you say that you need to have the device on.
  Android中通过各种Lock锁对电源进行控制,需要注意的是加锁和解锁必须成对出现。先上一段Standup Timer里的代码然后进行说明

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;
}
}


acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。

@Override
protected void onResume()
{
super.onResume();
//获取锁,保持屏幕亮度
acquireWakeLock();
startTimer();
}

 

protected void onPause()
{
super.onPause();
synchronized(this) {
cancelTimer();
releaseWakeLock();

if (finished) {
clearState();
} else {
saveState();
}
}
}

PowerManager和WakeLock的操作步骤

   1.   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。
   2.   然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
   3.   获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。

关于int flags
各种锁的类型对CPU 、屏幕、键盘的影响:

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.

权限获取
要进行电源的操作需要在AndroidManifest.xml中声明该应用有设置电源管理的权限。

<uses-permission android:name="android.permission.WAKE_LOCK" />

你可能还需要

<uses-permission android:name="android.permission.DEVICE_POWER" />

另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。

===============================================================
Android中如何让手机屏幕不待机

转载自:http://hi.baidu.com/piaochen/item/1234aa0e2bd74d3bf2eafce7

在Android中,申请WakeLock可以让你的进程持续执行即使手机进入睡眠模式,比较实用的是比如后台有网络功能,可以保证操作持续进行.

方法: 在操作之前加入

            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
            wakeLock.acquire();

其中newWakeLock有两个参数,第一个参数定义了行为,第二个参数是调试的那个Tag,可以定义为类名。第一个参数取值如下:
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
别忘了在操作完毕之后释放掉

      if (wakeLock != null) {
                wakeLock.release();
                wakeLock = null;
       }

还有,要加权限:<uses-permission android:name="android.permission.WAKE_LOCK"/>

不过我看博客,有篇文章说这个方法更省电,更安全一点,文章如下:

<uses-permission android:name="android.permission.WAKE_LOCK" />

   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);   
   PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");   
   mWakeLock.acquire();   
    // ...   
   mWakeLock.release();   


不过这个参数要求很大的耗电量 所以使用时候要注意可以参看PowerManager service.

另一种方法是

@Override  
      protected void onCreate(Bundle icicle) {   
          super.onCreate(icicle);   
     
           getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   
       }  

这个参数也是保持屏幕一直活动不会进入休眠状态,省电一些比上个安全。
还有人说应该这样写,没有手机,测试不了,郁闷:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);。

参考原帖地址:http://th8410.javaeye.com/blog/716731
           http://wang-peng1.javaeye.com/blog/580756

posted @ 2012-06-28 15:26  日光之下无新事  阅读(656)  评论(0编辑  收藏  举报