5

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!

Jameson
  • 6,400
  • 6
  • 32
  • 53
  • Google analytics for android tries to track the average time spent in an Activity. Have you looked into it? – Shellum Mar 19 '12 at 23:33
  • Hi Chuck, thanks for bringing this up. That is a step too fine of resolution, though, I think (on a continuum to per-app, within-app, and then within-activity-within-app.) – Jameson Mar 20 '12 at 00:26

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare, thank you for the reply! Yes, my intention was that both would involve a custom framework.jar. My use case is not applications development -- I'd prefer not to modify any App code if possible (and then there are of course cases when it'd not be possible anyhow.) WRT #1, I didn't mean to extend Activity within an app, but rather to directly and barbarically edit the Activity.java implementation. – Jameson Mar 20 '12 at 00:24
  • 1
    @Jameson: Ah, OK. Most people asking questions on SO are doing normal SDK development. If you are creating custom firmware, your option #1, or perhaps backporting the `Application.ActivityLifecycleCallbacks` and using those, would probably meet your needs. The danger with #2 is that you don't know if there are other code paths that start up activities that don't go through `startActivity()`. – CommonsWare Mar 20 '12 at 00:30