开发者

How to calculate the total time an activity has been on screen?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜