13

For my application, I need to know that the screen is locked. How to check this is problematically. I used following flag:

if(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON != 0){
    // some code
}else if((WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)!= 0){
   // some code
}

But this always executing both if and else part... which flag I have to use to check the screen is locked or not?

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
Prasad
  • 1,375
  • 5
  • 17
  • 31
  • See following links [http://stackoverflow.com/questions/3170563/android-detect-phone-lock-event][1] [http://stackoverflow.com/questions/8317331/detecting-when-screen-is-locked][2] [http://stackoverflow.com/questions/3446202/android-detect-phone-unlock-event-not-screen-on][3] [1]: http://stackoverflow.com/questions/3170563/android-detect-phone-lock-event [2]: http://stackoverflow.com/questions/8317331/detecting-when-screen-is-locked [3]: http://stackoverflow.com/questions/3446202/android-detect-phone-unlock-event-not-screen-on – Shruti Jan 25 '12 at 11:41
  • 5
    how it will execute both if and else ? – Yugandhar Babu Jan 25 '12 at 11:42
  • http://stackoverflow.com/questions/8968265/android-auto-logout-when-app-goes-to-background/8968763#8968763 – Padma Kumar Jan 25 '12 at 13:27

6 Answers6

23

I'll try to answer this though the question is already old since it is unresolved and could help other googlers. ;)

First you must register a BroadcastReceiver for Intent.ACTION_SCREEN_OFF & Intent.ACTION_SCREEN_ON. Note that this receiver must be registered in codes and will not work when declared in the manifest.

In your broadcast receiver, when you receive Intent.ACTION_SCREEN_ON, you can check if the screen is locked by using the below codes:

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
Macchiato
  • 825
  • 1
  • 11
  • 24
  • 1
    below link seems that does not need BroadcastReceiver http://stackoverflow.com/a/8668648/401403 – Arash Nov 03 '14 at 07:21
3
KeyguardManager myKeyManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);

if( myKeyManager.inKeyguardRestrictedInputMode()) {

 //screen is locked

} else {

 //screen is not locked

}
Vaishali Sutariya
  • 5,093
  • 30
  • 32
1

I guess you may have already found the answer, but if not (and for other developers), you can do it like this:

              PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
              boolean isScreenOn = powerManager.isScreenOn();

if (!isScreenOn) {
           //Screen is in OFF State
           //Code to power on and release lock 



                KeyguardManager km = (KeyguardManager) this
                 .getSystemService(Context.KEYGUARD_SERVICE);
               final KeyguardManager.KeyguardLock kl = km
                 .newKeyguardLock("MyKeyguardLock");
               kl.disableKeyguard();

               PowerManager pm = (PowerManager) this
                 .getSystemService(Context.POWER_SERVICE);
               WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
               wakeLock.acquire();
}
Ann
  • 727
  • 1
  • 7
  • 19
1

Register a broadcast receiver with action android.intent.action.ACTION_SCREEN_OFF and write your code in onReceive() method of receiver.

If you are using an activity, onPause() will be called when the screen locked and onResume() will be called when the screen unlocked.

In your code you are checking some flags, i don't know where you will do that checking ? is it continuous verification ? If you are using an activity in your app, the above procedure will happen, just check it in Android Developers website.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
0

Here is what I did:

This handles if the user has unlocked the screen, but not yet entered the home screen or the user's screen is turned off say during a call.

           if (Intent.ACTION_SCREEN_ON.equals(pIntent.getAction()) ||  
                     Intent.ACTION_USER_PRESENT.equals(pIntent.getAction())) {
                if(mListener!=null) {
                    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
                    boolean locked = km.inKeyguardRestrictedInputMode();
                    Log.v(TAG, ": Phone lock state from KEYGUARD_SERVICE: Current state:" + (locked ? "LOCKED":"UNLOCKED"));
                    mIsPhoneLocked = locked;
                }
            }
Akshat
  • 4,515
  • 4
  • 27
  • 28
0

There are broadcasted intents for screen lock & unlock.

Check it like :

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){//LOGIC Here}

Let me know!

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
LordKakarot
  • 108
  • 4