点击的通知后,已禁用键盘保护锁重新启用自己 [英] Disabled Keyguard Lock re-enables itself after clicking on a notification
问题描述
在我的应用程序禁止使用下面的code中的键盘保护锁(ieRemove锁屏),它正常工作,直到我点击通知栏的任何通知。如果我点击的通知锁屏会自动重新启用。任何帮助是AP preciated。
私人无效remove_lockscreen(){
最终的复选框preference锁=(复选框preference)找到preference(remove_lockscreen);
KeyguardManager公里=(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock KL = km.newKeyguardLock(keyguard_lock);
如果(lock.isChecked()){
prefEdit(remove_lockscreen,1);
Toast.makeText(getBaseContext(),锁屏将不会显示,Toast.LENGTH_SHORT).show();
kl.disableKeyguard();
}
否则如果(!lock.isChecked()){
prefEdit(remove_lockscreen,0);
Toast.makeText(getBaseContext(),锁屏将显示,Toast.LENGTH_SHORT).show();
kl.reenableKeyguard();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
我已经注意到了同样的问题在一段时间。它只发生在蜂窝(安卓3.0)及以上。经过大量的试验和拔头发,我似乎找到了一个解决方案,为我工作。目前还不清楚究竟发生了什么,或者为什么,但这里是我已经想通了。
看来,在Android 3.0+,后键盘锁被禁用,当通知是pressed,老KeyguardLock到期,但幸运的 ACTION_USER_ preSENT
广播被激发在这一点上,所以我们有机会来解决此问题。
有一点是一点都没有明显的文档,它似乎有必要得到一个新的,再禁用它之前重新启用旧KeyguardLock。另外疑难杂症我发现的是,通过旧的重新启用后,立即禁止通过新KeyguardLock只产生间歇性的成功。我通过禁用前等待300毫秒解决了这个。
下面是我的code略微简化版本;它应该很容易适应你的应用程序:
私人KeyguardLock KL;
私人KeyguardManager公里;
私人最终处理程序mHandler =新的处理程序();
私人最终可运行runDisableKeyguard =新的Runnable(){
公共无效的run(){
KL = km.newKeyguardLock(getPackageName());
kl.disableKeyguard();
}
};
私人无效setEnablednessOfKeyguard(布尔启用){
如果(启用){
如果(KL!= NULL){
unregisterReceiver(MUSER presentReceiver);
mHandler.removeCallbacks(runDisableKeyguard);
kl.reenableKeyguard();
KL = NULL;
}
} 其他 {
如果(km.inKeyguardRestrictedInputMode()){
registerReceiver(MUSER presentReceiver,用户present);
} 其他 {
如果(KL!= NULL)
kl.reenableKeyguard();
其他
registerReceiver(MUSER presentReceiver,用户present);
mHandler.postDelayed(runDisableKeyguard,300);
}
}
}
私人最终的BroadcastReceiver MUSER presentReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(Intent.ACTION_USER_ preSENT.equals(intent.getAction())){
如果(sp_store.getBoolean(KEY_DISABLE_LOCKING,FALSE))
setEnablednessOfKeyguard(假);
}
}
};
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 on a notification the lock screen is automatically re-enabled. Any help is appreciated.
private void remove_lockscreen() {
final CheckBoxPreference lock = (CheckBoxPreference) findPreference("remove_lockscreen");
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock kl = km.newKeyguardLock("keyguard_lock");
if (lock.isChecked()) {
prefEdit("remove_lockscreen", 1);
Toast.makeText(getBaseContext(), "Lockscreen will not be shown", Toast.LENGTH_SHORT).show();
kl.disableKeyguard();
}
else if (!lock.isChecked()) {
prefEdit("remove_lockscreen", 0);
Toast.makeText(getBaseContext(), "Lockscreen will be shown", Toast.LENGTH_SHORT).show();
kl.reenableKeyguard();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
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;
private final Handler mHandler = new Handler();
private final Runnable runDisableKeyguard = new Runnable() {
public void run() {
kl = km.newKeyguardLock(getPackageName());
kl.disableKeyguard();
}
};
private void setEnablednessOfKeyguard(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);
}
}
}
private final BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())){
if (sp_store.getBoolean(KEY_DISABLE_LOCKING, false))
setEnablednessOfKeyguard(false);
}
}
};
这篇关于点击的通知后,已禁用键盘保护锁重新启用自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!