Android: get time spent on activity

Firstly, I’m not looking for the time spent on this application. There is already an application for this, com.android.settings/.UsageStats and a large support code in AOSP frameworks/base/services/java/com/android/server/am/UsageStatsService.java , etc.

The code I have studied so far does not seem to record the elapsed time spent on specific <activity> s. I thought there were two ways to get this information, but I feel that there should be something cleaner and simpler that uses more existing code. The ideas were:

  • Apply the base class Activity class onPause() and onResume() to crack the timestamp and register the information in some place (possibly the SQLite database).
  • Tool Context class to take startActivity() when calling startActivity() and friends.

So what do you think is anything better than these options? Thank you in advance!

+4
source share
1 answer

So what do you think is anything better than these options?

Anything better than # 2, which requires special firmware.

# 1 is your only option in the SDK for API level 13 in down AFAIK.

API level 14 (aka, Android 4.0) has been added to Application.ActivityLifecycleCallbacks , which you can register with registerActivityLifecycleCallbacks() called on your Application (e.g. getApplicationContext() ). I have not used them yet, but it seems that you can organize a notification of one of the listeners about upcoming and upcoming actions, avoiding forcing to extend some common base class of Activity with the required logging.

+2
source

Source: https://habr.com/ru/post/1402334/


All Articles