Reputation: 173
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).
Upvotes: 2
Views: 1456
Reputation: 560
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();
Upvotes: 4