我正在尝试在我的应用程序中创建一个选项(使用复选框首选项),以供用户禁用和重新启用锁定屏幕。我使用 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();
}
}