0

How to detect if display is on?

Requirements

  • Must work on devices with API level >=17 and <20 (Android 4.2.2, 4.3, 4.4)
  • I am not asking for interactive state like Intent.ACTION_SCREEN_ON / PowerManager#isScreenOn() / #isInteractive()
  • Must work from inside a Service
OneWorld
  • 17,512
  • 21
  • 86
  • 136

2 Answers2

0

With DisplayManager for example

DisplayManager dm = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
    if (display.getState() != Display.STATE_OFF) {
        return true;
    }
}
return false;
OneWorld
  • 17,512
  • 21
  • 86
  • 136
An-droid
  • 6,433
  • 9
  • 48
  • 93
  • display.getState() requires API 20 – OneWorld Feb 10 '16 at 11:44
  • You are right, maybe use the DisplayManagerCompat from the supportv4 library ? It should behave the same way – An-droid Feb 10 '16 at 11:48
  • Unfortunately [DisplayManagerCompat#getDisplays()](http://developer.android.com/reference/android/support/v4/hardware/display/DisplayManagerCompat.html#getDisplays%28%29) returns [android.view.Display](http://developer.android.com/reference/android/view/Display.html) and not some _DisplayCompat_ I'd need Display#getState() which requires API 20 – OneWorld Feb 10 '16 at 12:35
-1

You can use a simple Broadcast receiver for screen sates (ON or OFF).Why don't you wanna use Intent.ACTION_SCREEN_ON ?.

Ref: enter link description here

Community
  • 1
  • 1
Hussnain Azam
  • 358
  • 1
  • 5
  • 14
  • The Broadcast Receiver receives Intent.ACTION_SCREEN_ON. But please prove me wrong by providing code. – OneWorld Feb 10 '16 at 11:46
  • in that link you can see the code of broadcast receiver for screen_on and screen_off – Hussnain Azam Feb 10 '16 at 11:50
  • And that receiver receives Intent.ACTION_SCREEN_ON – OneWorld Feb 10 '16 at 11:52
  • yea it receives.You dont want receiver to receive this (screen_on) event ?? – Hussnain Azam Feb 10 '16 at 11:54
  • No, because [doc](http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON) of this event says _"For historical reasons, the name of this broadcast action refers to the power state of the screen but it is actually sent in response to changes in the overall interactive state of the device. This broadcast is sent when the device becomes interactive which may have nothing to do with the screen turning on. To determine the actual state of the screen, use getState(). "_ (getState() requires API 20) – OneWorld Feb 10 '16 at 12:29
  • use can use DisplayManager for version greater then kitkat and PowerManager for version below – Hussnain Azam Feb 10 '16 at 12:40
  • I know, but [PowerManager#isScreenOn()](http://developer.android.com/reference/android/os/PowerManager.html#isScreenOn%28%29) and PowerManager#isInteractive() provide me the interactive state. I want the display state. – OneWorld Feb 10 '16 at 12:44
  • SensorEventListener is also an option you can get brightness value in that listener and on the base of that we you can detect screen visiblity – Hussnain Azam Feb 10 '16 at 12:50
  • Please post me code of your idea of SensorEventListener I am not aware off how to detect screen ON/OFF state through this. – OneWorld Feb 10 '16 at 12:53