Android判斷螢幕狀態與螢幕解鎖和鎖定

安卓世界庫發表於2016-02-20
主動判斷螢幕是否亮著:

public boolean isScreenOn(Context context) { 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    if (pm.isScreenOn()) { 
        return true; 
    } 

    return false; 

//螢幕解鎖 
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE); 
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(LOCK_TAG); 
keyguardLock.disableKeyguard(); 
//螢幕鎖定 
keyguardLock.reenableKeyguard(); 


KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);  
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");  
        //解鎖  
        kl.disableKeyguard();  
        //獲取電源管理器物件  
        PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);  
        //獲取PowerManager.WakeLock物件,後面的引數|表示同時傳入兩個值,最後的是LogCat裡用的Tag  
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");  
        //點亮螢幕  
        wl.acquire();  
        //釋放  
        wl.release();  

相關文章