【问题标题】:Disabled Keyguard Lock re-enables itself after clicking on a notification单击通知后,已禁用的 Keyguard Lock 会重新启用自身
【发布时间】:2012-10-14 16:26:45
【问题描述】:

在我的应用程序中,我使用下面的代码禁用了键盘锁(即删除锁屏),它可以正常工作,直到我点击通知栏中的任何通知。如果我单击通知,锁定屏幕会自动重新启用。任何帮助表示赞赏。

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());
    }
}

【问题讨论】:

    标签: android notifications lockscreen keyguard


    【解决方案1】:

    我注意到同样的问题已经有一段时间了。它只发生在 Honeycomb (Android 3.0) 及更高版本上。经过大量的实验和拉扯,我似乎找到了适合我的解决方案。目前尚不清楚到底发生了什么或为什么,但这是我想通的。

    似乎在 Android 3.0+ 上,禁用键盘保护后,当按下通知时,旧的 KeyguardLock 过期,但幸运的是 ACTION_USER_PRESENT 广播此时被触发,所以我们有机会纠正这个问题.

    文档中根本不明显的一点是,似乎有必要在获得新的 KeyguardLock 并再次禁用之前重新启用旧的 KeyguardLock。我发现的另一个“陷阱”是,在重新启用旧的 KeyguardLock 后立即禁用新的 KeyguardLock 只会产生间歇性的成功。我通过在禁用前等待 300 毫秒解决了这个问题。

    这是我的代码的稍微简化的版本;它应该很容易适应您的应用:

    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);
            }
        }
    };
    

    【讨论】:

    • +1 来自我。节省了我的一天 - 我只是对上面的代码做了一些小的修改(在 if 块之后移动了 postDelayed 行)。
    • @JalpeshKhakhi 我还没有在这些上测试过。应该,但如果 Android 已经被改变得足够多,它可能不会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多