Failed to re-enable KeyGaurd after disabling it.

I am writing below code to switch KeyGaurdmy Android phone using the toggle button. but I came across strange behavior.

  • It successfully disables KeyGaurd, but. not re-enable.

     btnToggleLock.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
    
            if (btnToggleLock.isChecked()) {
    
                toast.cancel();
                toast.setText("Unlocked");
                toast.show();
    
                Log.i("Unlocked", "If");
    
                KeyguardManager myKeyGuard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    
                KeyguardLock myLock = myKeyGuard
                        .newKeyguardLock(KEYGUARD_SERVICE);
                myLock.disableKeyguard();
    
    
            } else {
    
                toast.cancel();
                toast.setText("Locked");
                toast.show();
    
                KeyguardManager myKeyGuard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
                KeyguardLock myLock = myKeyGuard
                        .newKeyguardLock(KEYGUARD_SERVICE);
                myLock.reenableKeyguard();
    
                Log.i("Locked", "else");
    
            }
    
        }
    });
    
  • It successfully disables KeyGaurd, but. not re-enable.

  • I tested that control passes both if, andelse

I also use android 2.2.1 motoralla milestone.

+2
source share
1 answer

, Lock (KeyGuardLock) , if. , " " , , .

, onClickListener. .. "if" onClickListener: -

KeyguardManager myKeyGuard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardLock myLock = myKeyGuard
                    .newKeyguardLock(KEYGUARD_SERVICE);
+1

All Articles