Reputation: 2662
How would I calculate the total time an activity has been displayed on the screen? It should be the total time it had been on screen, across its various launches, since it has been installed.
Upvotes: 0
Views: 2745
Reputation: 11422
Use the activity lifecircle callback functions to detect what happend to your activity: http://developer.android.com/reference/android/app/Activity.html
The information how long an activity lived can be stored using the SharedPreferences: http://developer.android.com/reference/android/content/SharedPreferences.html
Upvotes: 1
Reputation: 8695
Keep a static time
variable. Start a new timer every time your activity is on screen, that is when onResume is called. And suspend the timer when your activity goes out of screen, that is onPause, onStop, onDestroy and add the time elapsed to the time
variable.
Copa's answer will be very useful to count the time through many sessions of your application.
Upvotes: 2