控制CPU、屏幕等是否休眠

獲取鎖

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,

                this.getClass().getCanonicalName());
    wakeLock.acquire();

  釋放鎖:
    if (wakeLock !=null&& wakeLock.isHeld()) {

        wakeLock.release();wakeLock=null;}

  參數說明: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.

  權限:<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
     可能還需要
<uses-permissionandroid:name="android.permission.DEVICE_POWER"/>


喚醒屏幕與解鎖:

    WakeLock screenLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
        | PowerManager.ACQUIRE_CAUSES_WAKEUP
        | PowerManager.ON_AFTER_RELEASE, TAG);

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
     KeyguardLock keyguardLock = km.newKeyguardLock(TAG);
      if (km.inKeyguardRestrictedInputMode()) {
       keyguardLock.disableKeyguard(); // 解開屏幕鎖

      }    

    keyguardLock.reenableKeyguard();  // 屏幕鎖生效


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章