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?
using KeyguardManager.NewKeyguardLock
is deprecated since API Level 13. Did you tried it with FLAG_DISMISS_KEYGUARD
and FLAG_SHOW_WHEN_LOCKED
of the WindowManager.LayoutParams.
This class was deprecated in API level 13. Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this allows >you to seamlessly hide the keyguard as your application moves in and out of >the foreground and does not require that any special permissions be requested. >Handle returned by newKeyguardLock(String) that allows you to disable / >reenable the keyguard.
Here the link form the Android documentation.
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With