Starksky
Starksky

Reputation: 41

disableKeyguard not disabling keyguard

I'm using this code to disable keyboard to disable home button . I'm using this in activity where I want the keyguard to be disabled ,so is it necessary that keyguard should be called from service ? If not why keyguard is not getting disabled ?

KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);

km.inKeyguardRestrictedInputMode();
this.key = km.newKeyguardLock("IN");
key.disableKeyguard();

String s = String.valueOf(km.isKeyguardLocked());
Log.d("keyguardvalue",s);

Upvotes: 1

Views: 462

Answers (1)

propoLis
propoLis

Reputation: 1283

Please call for activity onResume or onCreate:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    setShowWhenLocked(true);
    setTurnScreenOn(true);

    if (keyguardManager != null)
        keyguardManager.requestDismissKeyguard(this, null);
    }
}

Upvotes: 0

Related Questions