Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Get Time Spent Per Activity

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!

like image 896
Jameson Avatar asked Nov 04 '22 03:11

Jameson


1 Answers

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.

like image 120
CommonsWare Avatar answered Nov 09 '22 04:11

CommonsWare