设置UI不可被锁住

    private void setUnlocked() 
    { 
        Window win = getWindow(); 
        WindowManager.LayoutParams winParams = win.getAttributes(); 
        winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
            
            //| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
        win.setAttributes(winParams); 
    } 

       private void setLocked() { 
        Window win = getWindow(); 
        WindowManager.LayoutParams winParams = win.getAttributes(); 
        winParams.flags &= (~WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
                & ~WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
                //& ~WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 
                & ~WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
        win.setAttributes(winParams); 
    }



解锁屏幕
		final KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("Charge");
		keyguardLock.disableKeyguard();// 禁止锁屏,包括图形锁和滑动锁,密码锁
		new Handler().postDelayed(new Runnable() {

			public void run() {    
				keyguardLock.reenableKeyguard(); // 放弃禁止锁屏
			}
		}, 15000);

        km  = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
        if(km.inKeyguardRestrictedInputMode()){//判断是否已经处于锁屏状态
            setUnlocked();// screen is locked,keeping this UI can not be locked
        } else {
            setLocked();// screen is unlocked,keeping this UI can be locked
        }

	public void resetLock() {
    	boolean pattern = Settings.Secure.putInt(getContentResolver(),
				Settings.Secure.LOCK_PATTERN_ENABLED, 0);
		boolean password = Settings.Secure.putLong(getContentResolver(), "lockscreen.password_type",
				DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
	}

不知道立即锁屏要如何做?DeviceManager总是出现Security异常

你可能感兴趣的:(UI)