其他分享
首页 > 其他分享> > Android:禁用键盘保护后无法重新启用它

Android:禁用键盘保护后无法重新启用它

作者:互联网

我正在尝试在我的应用程序中创建一个选项(使用复选框首选项)供用户禁用和重新启用锁定屏幕.我使用disableKeyguard()禁用锁定屏幕,它可以完美工作,但是我无法使reenableKeyguard()正常工作.该代码非常简单,我不知道为什么它不起作用.

public void onSharedPreferenceChanged(SharedPreferences taskprefs,
        String tasks_pref) {
    boolean skiplock = taskprefs.getBoolean("pref_skiplock", false);
    boolean screentimeout = taskprefs.getBoolean("pref_screentimeout",
            false);

    skiplock(skiplock);

    // Log.v("TaskActivity", "Skiplock value is " + skiplock);
    // Log.v("TaskActivity", "ScreenTimeout value is " + screentimeout);
}

private void skiplock(boolean action) {
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    //
    if (action == true) {
        lock.disableKeyguard();
        Toast.makeText(getApplicationContext(), "Lockscreen Disabled",
                Toast.LENGTH_SHORT).show();
    }
    //
    else if (action==false) {
        lock.reenableKeyguard();
        Toast.makeText(getApplicationContext(), "Lockscreen Enabled",
                Toast.LENGTH_SHORT).show();
    }
}

解决方法:

问题是每次调用一次时,都会在skiplock()方法内部创建(声明)一个新的Keyguard对象.一个简单的解决方案是将键盘锁声明为全局对象,并引用该单个对象,以便在方法完成执行时不会重置其状态.

简而言之,只需将“ KeguardLock锁”和“ KeyguardManager keyguardManager”用作全局构造函数即可.

标签:locking,screen,preferences,android,keyguard
来源: https://codeday.me/bug/20191101/1982587.html