25

I need to track app usage in Android like how AppUsage does it (i.e. track the number of app launches, and time spent using each). What I plan on doing is recording the start time when an app is launched, and then getting the end time when an app is paused or stopped. In LogCat, I'm able to see logs of Activity starting. I've looked at the available system broadcasts, I'm not sure if there is anything that broadcasts whenever an activity is started. Basically, how do I detect when an activity is launched?

Edit: Moreover, Android also has a usage statistics system available internally which can be found in the Spare Parts app or when *#*#4636#*#* is dialed (Testing -> Usage statistics)

sofiaguyang
  • 1,123
  • 1
  • 14
  • 21

3 Answers3

17

We can now access app usage history on an Android device with the new android.app.usage API released as part of Android 5.0 APIs.The system collects the usage data on a per-app basis, aggregating the data over daily, weekly, monthly, and yearly intervals

For each app, the system records the following data:

  • The last time the app was used
  • The total length of time the app was in the foreground for that time interval (by day, week, month, or year)
  • Timestamp capturing when a component (identified by a package and activity name) moved to the foreground or background during a day

  • Timestamp capturing when a device configuration changed (such as when the device orientation changed because of rotation)

Refr : http://developer.android.com/reference/android/app/usage/package-summary.html

Jorn Rigter
  • 745
  • 1
  • 6
  • 25
android.fryo
  • 1,469
  • 1
  • 14
  • 18
6

Basically, how do I detect when an activity is launched?

You don't. There is nothing in the Android SDK for "the start time when an app is launched" nor "the end time when an app is paused or stopped".

What the app you cite is probably doing is wasting a lot of CPU time, RAM, and battery life, polling ActivityManager continuously.

Bear in mind that what you propose to track, if you plan on having anyone other than the user access it, borders on privacy violations of the type that got CarrierIQ in a fair amount of trouble.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    According to [AppUsage description from the android market](https://market.android.com/details?id=com.smartappers.appusage&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5zbWFydGFwcGVycy5hcHB1c2FnZSJd): AppUsage _uses virtually no battery power- only records when you change apps, unlike others which check every x seconds!_ - I'm not sure how AppUsage is doing this. This is for a thesis project which recommends apps to users. One of the requirements is to track app usage. – sofiaguyang Jan 26 '12 at 01:02
  • 2
    @Sofia: I will take a look at their app and report to Google if they are exploiting a security flaw. Thank you for bringing this to my attention! – CommonsWare Jan 26 '12 at 01:26
  • 2
    Internally, Android also has a usage statistics system available which can be found in the Spare Parts app or when `*#*#4636#*#*` is dialed. Testing -> Usage statistics – sofiaguyang Jan 26 '12 at 03:02
  • 1
    Yes, I'm aware that the built-in usage statistics is not part of the SDK. I initially planned to hack and retrieve the android.jar available in the phone to gain access to internal and hidden APIs, but decided to settle with polling the `ActivityManager` instead. – sofiaguyang Jan 26 '12 at 15:00
  • 21
    "You don't" is not a very good answer. There are legitimate approaches and use-cases for almost anything, and the OP has provided one in the question itself. – Nick Jul 23 '12 at 12:20
  • 1
    @Nick: You have a very privacy-challenged concept of "legitimate approaches". Android has blocked SDK app access to the `READ_LOGS` permission in Jelly Bean in part to help prevent privacy issues, and some comments at the 2012 Google I|O conference suggest that they might turn their attention to `ActivityManager` next. There are certainly ways the Android SDK could support apps like this in ways that reduce privacy concerns (e.g., making it part of the device admin APIs, which require extra opt-in steps beyond app installation). However, this has not been added yet. – CommonsWare Jul 23 '12 at 12:45
  • 1
    An app making use of app usage statistics doesn't need to have anything to do with privacy. Consider an app that doesn't even request the `INTERNET`-permission and where users download the app for the specific feature of being able to collect usage statistics (as is the case with the AppUsage-app, although it's not a very good example, as they probably sell the usage data ;-)...). I'm not saying that it *can't*, ever, be used for malicious purposes, but then again everything can. I know of the change in Jelly Bean and personally think that it's the wrong approach. – Nick Jul 23 '12 at 13:28
  • @Nick: But that does change my answer. There is nothing in the Android SDK for "the start time when an app is launched" nor "the end time when an app is paused or stopped". You are welcome to prove otherwise. – CommonsWare Jul 23 '12 at 13:36
  • 4
    I agree, but that doesn't mean the answer is "You don't". Rather "The Android-team did not provide you with easy-to-use ways to do this", but there are of course (non-loophole-exploiting) ways to do something for almost anything (in this case, logcat-scanning on pre-4.1 devices). You're right that this puts a strain on battery-life and CPU usage, but it's probably closer to the answer that the OP was looking for than "No." – Nick Jul 23 '12 at 13:53
1

This might not fit your needs, but as for me in 2023, I didn't want to do much work and headaches, so all the thanks to the Firebase team for providing free Analytics service. Check this link for more information.