My app needs to know when the user last interacted with the phone, or when the device screen went off. Answers to similar questions suggested:
- registering a
SCREEN_OFF
/DEVICE_IDLE_MODE_CHANGED
broadcast receiver in manifest.
Problem: won't work since it's a protected broadcast - dynamically registering and unregistering
SCREEN_OFF
/DEVICE_IDLE_MODE_CHANGED
when an app component is live
Problem: I can't or don't want to keep the app or service running since I need to know SCREEN_OFF 24x7. - Using
ActivityManager
orUsageStats
Problem: Gives details only for particular packages so to be able to figure out when the app was last used, will have to iterate over a list and make many function calls. And even after that, I am not sure if this covers the case when no app is used but only a quick device unlock and relock action.
Is there a way to reliably know in the background when the user last used their device without keeping a service running?