Android:获取每个活动花费的时间 [英] Android: Get Time Spent Per Activity
问题描述
首先,我不是在寻找在给定应用程序上花费的时间. 在AOSP frameworks/base/services/java/com/android/server/am/UsageStatsService.java
中已经存在用于此目的的应用程序",com.android.settings/.UsageStats
和大量支持代码.
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.
到目前为止,我检查过的代码似乎并未记录在特定<activity>
s 上花费的时间.我曾想过以两种方式获取此信息,但是我觉得必须有一些更干净,更简单的东西,可以利用更多现有代码.这些想法是:
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:
- 插入基类
Activity
和onResume()
,以破解时间戳,并将信息记录到某个位置(可能是SQLite数据库). - 插入
Context
类,以便在每次调用startActivity()
和朋友时记录下来.
- Instrument the base
Activity
classonPause()
andonResume()
, to hack in a timestamp, and log the info some place (probably a SQLite database.) - Instrument the
Context
class, to make note wheneverstartActivity()
and friends are called.
那么,您认为呢?还有什么比那些选择更好的呢?预先谢谢你!
So what do you think -- anything better than those options? Thank you in advance!
推荐答案
那么,您认为呢?有什么比那些选择更好的了?
So what do you think -- anything better than those options?
任何事情都比#2更好,后者需要自定义固件.
Anything is better than #2, which requires custom firmware.
#1是向下AFAIK上API级别13的SDK中的唯一选项.
#1 is your only option within the SDK for API Level 13 on down AFAIK.
API级别14(又称为Android 4.0),您可以通过在Application
(例如,getApplicationContext()
)上调用的registerActivityLifecycleCallbacks()
进行注册.我还没有使用过这些,但是看来您可以安排一个单独的侦听器来通知即将发生的活动,从而避免强迫您使用所需的日志记录扩展某些常见的Activity
基类.
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.
这篇关于Android:获取每个活动花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!