我有一个功能可以确定手机屏幕的四种状态:屏幕开启、屏幕关闭、屏幕开启并锁定、屏幕开启且未锁定。我的功能是
private KeyguardManager keyguardManager;
public String getScreenStatus()
{
String sreen_State="unknown";
keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
if (pm.isInteractive()) {
sreen_State="screen_on";
if(!keyguardManager.inKeyguardRestrictedInputMode()) {
sreen_State="screen_on_no_lock_screen";
}else{
Log.i(TAG, "screen_on_lock_screen");
sreen_State="screen_on_lock_screen";
}
}
else {
sreen_State="screen_off";
}
}
else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH){
if(pm.isScreenOn()){
sreen_State="screen_on";
if(!keyguardManager.inKeyguardRestrictedInputMode()) {
Log.i(TAG, "screen_on_no_lock_screen");
sreen_State="screen_on_no_lock_screen";
}else{
Log.i(TAG, "screen_on_lock_screen");
sreen_State="screen_on_lock_screen";
}
}
else {
mIsScreenOn=false;
sreen_State="screen_off";
}
}
return sreen_State;
}
上述函数返回屏幕的正确状态。但是,当我再添加一个代码时,它会出现错误,如下所示:
KeyguardManager.KeyguardLock
kl = keyguardManager.newKeyguardLock("MyKeyguardLock");
if(index.equals("1"))
kl.disableKeyguard();
else if(indexequals("2"))
kl.reenableKeyguard();
getScreenStatus();
索引可以通过按一个按钮来改变。现在,屏幕的错误状态发生了。它总是返回screen_on_lock_screen
,尽管屏幕在screen_on_no_lock_screen
。我该如何解决我的问题?