Android: Get Time Spent Per Activity

安稳与你 提交于 2019-12-22 08:59:19

问题


Firstly, I'm not looking for time spent on a given application. There is already "an app for that", com.android.settings/.UsageStats, and a good deal of supporting code in the AOSP frameworks/base/services/java/com/android/server/am/UsageStatsService.java, etc.

The code that I've examined so far does not seem to record elapsed time spent on particular <activity>s. I have thought to get this information two ways, but feel there must be something cleaner and simpler, that leverages more existing code. The ideas have been:

  1. Instrument the base Activity class onPause() and onResume(), to hack in a timestamp, and log the info some place (probably a SQLite database.)
  2. Instrument the Context class, to make note whenever startActivity() and friends are called.

So what do you think -- anything better than those options? Thank you in advance!


回答1:


So what do you think -- anything better than those options?

Anything is better than #2, which requires custom firmware.

#1 is your only option within the SDK for API Level 13 on down AFAIK.

API Level 14 (a.k.a., Android 4.0) added in Application.ActivityLifecycleCallbacks, which you can register via registerActivityLifecycleCallbacks() called on your Application (e.g., getApplicationContext()). I haven't used these yet, but it would appear that you can arrange for a single listener to be notified of activities coming and going, avoiding forcing you to extend some common base Activity class with your desired logging.



来源:https://stackoverflow.com/questions/9778990/android-get-time-spent-per-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!