2

Is there a way to determine if a user is actively present on phone (not just screen on).

I cannot depend on catching events. These only fire off at points in time within a BroadcastReceiver:

intent.getAction().equals(Intent.ACTION_USER_PRESENT)
intent.getAction().equals(Intent.ACTION_SCREEN_ON)

I need a simple way to perform an active check in that moment. A service is firing off, and I do not want it to fire off while the phone is active (screen on).

XMAN
  • 176
  • 1
  • 3
  • 12

1 Answers1

4

You can check it through PowerManager class of android SDK.

   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   boolean isScreenOn = pm.isScreenOn();

Updated answer: isScreenOn() method is deprecated, Replace it with isInteractive()

   boolean isScreenOn = pm.isInteractive();
MezzDroid
  • 560
  • 4
  • 11
  • Genius! Thanks- looks like this will do the trick. I'll report back if it does not. – XMAN Nov 15 '17 at 21:01
  • isScreenOn() is deprecated in favor of isInteractive() [link](https://stackoverflow.com/questions/30718783/andorid-is-screen-on-or-off-deprecated-isscreenon). – XMAN Nov 15 '17 at 21:10