Android WakeLock 使用总结

目的:

  解决屏幕唤醒问题

遇到的问题:

  调用wake.release后,程序退出及其它程序都保持唤醒状态,无法恢复到系统自由控制唤醒。

解决办法:

  调用wake.release后,将wake == null;

 

贴代码:

 1 package com.example.boke;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.os.PowerManager;
 6 import android.os.PowerManager.WakeLock;
 7 
 8 public class BaseActivity extends Activity {
 9 
10     /**
11      * 系统电量管理对象
12      */
13     protected PowerManager powerManager = null;
14     /***
15      * 唤醒操作管理对象
16      */
17     protected WakeLock wakeLock = null;
18     /**
19      * 是否使用唤醒,标志位
20      */
21     private boolean isWakeUp = false;// 屏幕是否常亮
22 
23     /**
24      * 是否启动界面长亮
25      * @param bEnable
26      */
27     public void enableWackUpActivity(boolean bEnable) {
28         if (bEnable) {
29             isWakeUp = true;
30             openWakeUp();
31         } else {
32             isWakeUp = false;
33             closeWakeUp();
34         }
35     }
36 
37     /**
38      * 打开界面长亮功能
39      */
40     private void openWakeUp() {
41         if (this.powerManager == null) {
42             this.powerManager = (PowerManager) this
43                     .getSystemService(Context.POWER_SERVICE);
44         }
45         if (this.wakeLock == null) {
46             this.wakeLock = this.powerManager.newWakeLock(
47                     PowerManager.FULL_WAKE_LOCK, "TNT");
48         }
49         if (!this.wakeLock.isHeld()) {
50             this.wakeLock.acquire();
51         }
52     }
53 
54     /**
55      * 关闭界面长亮功能
56      */
57     private void closeWakeUp() {
58         if (this.wakeLock != null) {
59             if (this.wakeLock.isHeld()) {
60                 this.wakeLock.release();
61                 this.wakeLock = null;
62             }
63         }
64     }
65 
66     @Override
67     protected void onPause() {
68         // TODO Auto-generated method stub
69         super.onPause();
70         if (isWakeUp) {
71             closeWakeUp();
72         }
73     }
74 
75     @Override
76     protected void onResume() {
77         // TODO Auto-generated method stub
78         super.onResume();
79         if (isWakeUp) {
80             openWakeUp();
81         }
82     }
83 
84 }
View Code

 

posted @ 2014-01-17 00:29  junqinghaha  阅读(1193)  评论(0编辑  收藏  举报