3

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:

  1. registering a SCREEN_OFF/DEVICE_IDLE_MODE_CHANGED broadcast receiver in manifest.
    Problem: won't work since it's a protected broadcast
  2. 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.
  3. Using ActivityManager or UsageStats
    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?

fsljfke
  • 431
  • 6
  • 14
  • 1
    AppUsageStats can provide the info regarding lock screen count as well as the timestamp, but you need to build your own logic on how you are going to retrieve the same. – Gautam Aug 28 '20 at 05:26

0 Answers0