我正在尝试在我的应用中创建一个选项(使用复选框首选项),以便用户禁用并重新启用锁定屏幕。我使用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();
}
}
答案 0 :(得分:4)
问题是每次调用skiplock()方法时都会创建(声明)一个新的Keyguard对象。一个简单的解决方案是将keyguard声明为全局对象并引用该单个对象,以便在方法完成执行时不会重置其状态。
简而言之,只需使用“KeguardLock lock”和“KeyguardManager keyguardManager”作为全局构造函数。