无法重新启用KeyGaurd一旦停用它 [英] Could not re-enable the KeyGaurd once disabled it
问题描述
下面我有code写的使用切换按钮来切换 KeyGaurd
我的Android手机。但我面临着一个奇怪的现象。
I have write below code to toggle the KeyGaurd
of my android phone using a toggle button. but I am facing an strange behavior.
-
它会禁用
keygaurd
成功,但。不重新启用。
it disables the
keygaurd
successfully but. not re-enabling.
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");
}
}
});
它会禁用 keygaurd
成功,但。不重新启用。
it disables the keygaurd
successfully but. not re-enabling.
也是我米采用了android 2.2.1 motoralla里程碑
。
also I m using android 2.2.1 motoralla milestone
.
推荐答案
这里的问题是,你正在创建一个新的锁(KeyGuardLock)每次if语句执行时间。您可以禁用锁在创建它的第一次,但你必须重新启用的你禁用摆在首位,你不能创建一个新的锁。
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.
的解决方案是使该锁的onClickListener之外。即采取以下code出了如果声明并设置onClickListener之前声明它: -
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:-
KeyguardManager myKeyGuard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock myLock = myKeyGuard
.newKeyguardLock(KEYGUARD_SERVICE);
这篇关于无法重新启用KeyGaurd一旦停用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!