Return a failure result in inKeyguardRestrictedInputMode()
Asked Answered
S

1

12

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?

Shandy answered 26/9, 2016 at 13:31 Comment(1)
Can you add more code? Which class and method contains the second code snippet?Harrison
H
2

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!

Harrison answered 5/10, 2016 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.