1. 程式人生 > >Android檢測手機鎖屏以及保持螢幕常亮

Android檢測手機鎖屏以及保持螢幕常亮

//保持手機常亮並解鎖
private void wakeAndUnlock() {
    //獲取電源管理器物件
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    //獲取PowerManager.WakeLock物件,後面的引數|表示同時傳入兩個值,最後的是除錯用的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
, "bright"); //點亮螢幕 wl.acquire(); //得到鍵盤鎖管理器物件 KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); kl = km.newKeyguardLock("unLock"); //解鎖 kl.disableKeyguard(); }
//檢測是否鎖屏
private boolean isScreenLocked() {
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE
); return keyguardManager.inKeyguardRestrictedInputMode(); }