开发者

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

开发者 https://www.devze.com 2023-04-05 07:10 出处:网络
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 install

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

精彩评论

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