Android喚醒、解鎖螢幕

丿灬安之若死發表於2017-11-02

解鎖、喚醒螢幕用到KeyguardManager,KeyguardLock,PowerManager,PowerManager.WakeLock
 
所需許可權:

複製程式碼程式碼如下:

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

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

 

複製程式碼程式碼如下:

//鎖屏、喚醒相關
private KeyguardManager  km;
private KeyguardLock kl;
private PowerManager pm;
private PowerManager.WakeLock wl;
 
 
 
private void wakeAndUnlock(boolean b)
{
       if(b)
       {
              //獲取電源管理器物件
              pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
 
              //獲取PowerManager.WakeLock物件,後面的引數|表示同時傳入兩個值,最後的是除錯用的Tag
              wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
 
              //點亮螢幕
              wl.acquire();
             
              //得到鍵盤鎖管理器物件
              km= (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
              kl = km.newKeyguardLock("unLock");
 
              //解鎖
              kl.disableKeyguard();
       }
       else
       {
              //鎖屏
              kl.reenableKeyguard();
             
              //釋放wakeLock,關燈
              wl.release();
       }
      
}

相關文章