2個重要的類
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
在manifest.xml中需要添加權限
android.permission.WAKE_LOCK
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");PowerManager的幾個方法
1.newWakeLock(int levelAndFlags,Strin tag);
LEVEL:有4個等級
PARTIAL_WAKE_LOCK 屏幕關,鍵盤燈關,不休眠
SCREEN_MID_WAKE_LOCK 屏幕灰,鍵盤燈關,不休眠
SCREEN_BRIGHT_WEEK_LOCK 屏幕亮,鍵盤燈關,不休眠
FULL_WAKE_LOCK 屏幕亮,鍵盤燈亮,不休眠
FLAGS:有2個
ON_AFTER_RELEASE
,ACQUIRE_CAUSES_WAKEUP
比如:
PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
2.boolean isScreenOn();當屏幕是亮的時候返回true;
3.wakeUp(long time);喚醒屏幕。一般是這樣設置 wakeUp(SystemClock.uptimeMillis());可立即喚醒屏幕
4.goToSleep (long time); goToSleep(SystemClock.uptimeMillis());可以將屏幕休眠。類似按下power鍵;
5.reboot(String reason) Reboot the device. Will not return if the reboot is successful. reason可以是"recovery"
PowerManager.WakeLock 的幾個方法 1.boolean isHeld() Returns true if the wake lock has been acquired but not yet released. 2.void release() Releases the wake lock. 3.void acquire() Acquires the wake lock 4.void acquire(long time) Acquires the wake lock with a timeout. 主要使用就這2個類了。