How to find the spent time of android application used in foreground. i have used UsageStatsManager but it doesn't return the list of packages.
sample code used from this link
Asked
Active
Viewed 142 times
0

Phantômaxx
- 37,901
- 21
- 84
- 115

jeevashankar
- 1,418
- 1
- 12
- 13
-
did you ask permission of PACKAGE_USAGE_STATS in manifest – Aravind V Mar 05 '19 at 08:47
-
yes i'm added PACKAGE_USAGE_STATS permission in manifest file – jeevashankar Mar 05 '19 at 09:06
1 Answers
1
I think the best option is to use lifecycle callback methods of your activities. The start time is onCreate() of the first activity you show to your user. The end time may be a bit more tricky. First of all, if you have multiple activities, user can exit any of them, so you need to track the currently opened activity and send the end-time only if the next activity doesn't appear. The second problem is which callback method should you use? onPause() isn't guaranteed to be called, and onDestroy() won't be called until the application is cleared from recents. So probablt onStop() is the right place.
Or you can use the callbacks of your application. Just derive from Application class and use it's callbacks for your time tracking.

RexSplode
- 1,475
- 1
- 16
- 24
-
thank u for your response and it's working . i have used activity lifecycle callbacks in application. – jeevashankar Mar 07 '19 at 07:34