目的:
解决屏幕唤醒问题
遇到的问题:
调用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 bEnable26 */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) this43 .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 @Override67 protected void onPause() {68 // TODO Auto-generated method stub69 super.onPause();70 if (isWakeUp) {71 closeWakeUp();72 }73 }74 75 @Override76 protected void onResume() {77 // TODO Auto-generated method stub78 super.onResume();79 if (isWakeUp) {80 openWakeUp();81 }82 }83 84 }