Skip to content Skip to sidebar Skip to footer

Disabled Keyguard Lock Re-enables Itself After Clicking On A Notification

In my application I disable the keyguard lock (i.e.Remove Lockscreen) using the code below and it works fine until I click on any notification in the notification bar. If I click o

Solution 1:

I've noticed the same issue for some time. It only occurs on Honeycomb (Android 3.0) and up. After a great deal of experimentation and hair-pulling, I seem to have found a solution that works for me. It's not clear exactly what's going on or why, but here's what I've figured out.

It seems that on Android 3.0+, after the keyguard is disabled, when a notification is pressed, the old KeyguardLock expires, but thankfully the ACTION_USER_PRESENT Broadcast is fired at that point, so we have a chance to correct the issue.

One point that's not at all obvious from the documentation is that it seems to be necessary to reenable the old KeyguardLock before getting a new one and disabling it again. Another "gotcha" I discovered is that disabling through the new KeyguardLock immediately after reenabling through the old one produces only intermittent success. I resolved this by waiting 300ms before disabling.

Here's a slightly simplified version of my code; it should be easy to adapt to your app:

private KeyguardLock kl;
private KeyguardManager km;

privatefinalHandlermHandler=newHandler();

privatefinalRunnablerunDisableKeyguard=newRunnable() {
    publicvoidrun() {
        kl = km.newKeyguardLock(getPackageName());
        kl.disableKeyguard();
    }
};

privatevoidsetEnablednessOfKeyguard(boolean enabled) {
    if (enabled) {
        if (kl != null) {
            unregisterReceiver(mUserPresentReceiver);
            mHandler.removeCallbacks(runDisableKeyguard);
            kl.reenableKeyguard();
            kl = null;
        }
    } else {
        if (km.inKeyguardRestrictedInputMode()) {
            registerReceiver(mUserPresentReceiver, userPresent);
        } else {
            if (kl != null)
                kl.reenableKeyguard();
            else
                registerReceiver(mUserPresentReceiver, userPresent);

            mHandler.postDelayed(runDisableKeyguard,  300);
        }
    }
}

privatefinalBroadcastReceivermUserPresentReceiver=newBroadcastReceiver() {
    @OverridepublicvoidonReceive(Context context, Intent intent) {
        if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())){
            if (sp_store.getBoolean(KEY_DISABLE_LOCKING, false))
                setEnablednessOfKeyguard(false);
        }
    }
};

Post a Comment for "Disabled Keyguard Lock Re-enables Itself After Clicking On A Notification"