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, acr开发者_JS百科oss its various launches, since it has been installed.
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.
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
精彩评论