Reputation: 171
I am trying to find the best way to keep track of how much time a user spent on an android application (to report it to a server). I am only interested in tracking time where the user seems to use the app, so if it's in the background i shouldn't count that time for example. I did some research and the only suggestion I found so far is using the onResume() and onPause() methods of activities to keep track of elapsed time between those 2 calls; I would probably need to do this in each activity of the application which doesn't seem to be a very elegant solution. Is there another way, or better way to keep track of this time?
thanks in advance for the help.
Upvotes: 1
Views: 922
Reputation: 234795
I don't think that there's anything better. If all your activities extend Activity
, then you can consolidate the code needed to track the time by having them extend your own custom Activity
subclass and put all the tracking logic there. It might also help to have your own Application subclass and put the shared data structures there for keeping track of time.
Upvotes: 4