I have a Phonegap Android app that scans RFID chips. Apparently scanning RFID does not count as a user action, because the device will go to sleep after the interval set in Android Settings (which is a maximum of 10 minutes).
So despite constant scanning, my device will go to sleep. So I need a way to tell the OS that I’m active, programtically after each RFID scan.
Currently, I am doing something that I don’t want, which is using
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
to prevent sleep while the app is running, but I’d like to still be able to use the sleep interval if the device is actually inactive.
I had high hopes for using powerManager.userActivity(l, false) but apparently this is only available for System apps, so I cannot set permission for it.
Any ideas how to keep the device active while not doing any touch screen interaction?
A WakeLock is the answer, but the key is to use
ON_AFTER_RELEASE. With this flag, the user activity timer will be reset when the WakeLock is released.So for the NFC plugin, under the onPause method I added:
And under the onResume method I added:
With this arrangement, when an RFID chip is scanned, the NFC plugin will create the WakeLock and then release it, which will reset the activity timer. Also, I used
SCREEN_BRIGHT_WAKE_LOCKeven though it is deprecated, as this will brighten the screen if it has gone dim.