2

I am attempting to determine whether or not the screen is on using the following code:

    private void isScreenOn() {
        if (Build.VERSION.SDK_INT >= 20) {
            if (mPowerManager.isInteractive()) {
                //Do stuff
            }
        } else {
            if (mPowerManager.isScreenOn()) {
                //Do stuff
            }
        }
    }

I've had reports from the field that this is being logged:

com.aws.android I/dalvikvm: Could not find method android.os.PowerManager.isInteractive, referenced from method ...

Can anyone help me understand how this could be occurring? The isScreenOn() method is supposed to be valid up to API 19 and isInteractive() is supposed to be valid for API 20 and up. Am I doing something wrong here?

neonDion
  • 2,278
  • 2
  • 20
  • 39

1 Answers1

4

I figured out the problem. I am targeting android SDK version 22. If you call PowerManager.isScreenOn() on a device running < version 20, then the isScreenOn call really just wraps the isInteractive call.

This was spelled out in the documentation but it didn't register when I read it.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
neonDion
  • 2,278
  • 2
  • 20
  • 39
  • 1
    Your post helped me realize why my APP was crashing with API 14. The if-then-else clause was a solid fix. – XMAN Feb 18 '18 at 07:23