Skip to content Skip to sidebar Skip to footer

Could Not Re-enable The Keygaurd Once Disabled It

I have write below code to toggle the KeyGaurd of my android phone using a toggle button. but I am facing an strange behavior. it disables the keygaurd successfully but. not re-

Solution 1:

The issue here is that you are creating a new Lock (KeyGuardLock) every time the "if" statement executes. You can disable a lock the first time when you create it but you must "reenable" the lock which you disabled in the first place, you cannot create a new one.

The solution is to make the lock outside of the onClickListener. i.e. Take the following code out of the "if" statement and declare it before setting the onClickListener:-

KeyguardManagermyKeyGuard= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardLockmyLock= myKeyGuard
                    .newKeyguardLock(KEYGUARD_SERVICE);

Post a Comment for "Could Not Re-enable The Keygaurd Once Disabled It"