Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if the screen is on in android?

Tags:

android

In Android 2.2 (Level 7) the function PowerManager.IsScreenOn() returns a boolean that is true if the screen is turned on and false if the screen is turned off. I am developing code for Android 1.5 (Level 3). How do I accomplish the same task in older versions of Android?

I do not want to turn the screen on or off in my code. I just want to know what it is.

like image 381
vandermude Avatar asked Mar 19 '10 00:03

vandermude


People also ask

How do I know if my Android is idle?

1.) Android 4.3 onwards(api 18 and above) :use "idle"( CPU Idle) and "load" ( CPU Load) as list category options. 2.) Android 4.2 and below (below api 18) : use "-i, --cpu-idle" to trace cpu idle events and "-l, --cpu-load" for cpu load percentage ( though for your need cpu idle events shall do the job ).


1 Answers

There's a better way than using BroadcastReceivers:

// If you use API20 or more: DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); for (Display display : dm.getDisplays()) {     if (display.getState() != Display.STATE_OFF) {         return true;     } } return false;  // If you use less than API20: PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); if (powerManager.isScreenOn()){ return true; } 

Note that Display.getState() can also return STATE_DOZE and STATE_DOZE_SUSPEND which means that the screen is on in an special way. More info on Display.getState() and his return values here: http://developer.android.com/reference/android/view/Display.html#getState()

Also note that although official documentation recommends using isInteractive() instead of isScreenOn(), if you really want to know the status of the screen, Display.getState() is a better option because of the 'special' conditions that sets the screen on while the device is not interactive.

like image 116
Jorge Fuentes González Avatar answered Oct 03 '22 05:10

Jorge Fuentes González