I have a function to determine four states of phone's screen: screen on, screen off, screen on with lock, screen on without lock. My function is
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;
}
The above function returns corrected states of the screen. However, it has error when I add one more code as follows:
KeyguardManager.KeyguardLock
kl = keyguardManager.newKeyguardLock("MyKeyguardLock");
if(index.equals("1"))
kl.disableKeyguard();
else if(indexequals("2"))
kl.reenableKeyguard();
getScreenStatus();
The index can change by press a button. Now, the wrong state of screen is happen. It always return screen_on_lock_screen
, although the screen is in screen_on_no_lock_screen
. How could I fix my issue?