有些场景需要程序自动点亮屏幕,解开屏幕锁,以方便用户即时操作,下面用代码来实现这一功能:

 
  1. //得到键盘锁管理器对象  
  2. KeyguardManager  km= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);  
  3. //参数是LogCat里用的Tag  
  4. KeyguardLock kl = km.newKeyguardLock("unLock");  
  5. //解锁  
  6. kl.disableKeyguard();  
  7.  
  8. //获取电源管理器对象  
  9. PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);  
  10. //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag  
  11. PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");  
  12. //点亮屏幕  
  13. wl.acquire();  
  14. //释放  
  15. wl.release(); 

需要在AndroidManifest.xml添加权限:

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

flags参数说明:

  1. PARTIAL_WAKE_LOCK: Screen off, keyboard light off 
  2. SCREEN_DIM_WAKE_LOCK: screen dim, keyboard light off 
  3. SCREEN_BRIGHT_WAKE_LOCK: screen bright, keyboard light off 
  4. FULL_WAKE_LOCK: screen bright, keyboard bright