Quickstart
Build a Flutter app that collects the installed apps and the app usage of the last day and prints each measurement.
The finished app collects the installed apps and the app usage of the past day once a day, and prints each measurement as JSON.
Prerequisites
- The package and its Android setup, as described in Installation.
- An Android phone. On iOS, the measures do not run.
Complete example
Replace the contents of lib/main.dart with:
How it works
| Stage | What it does |
|---|---|
| 1. Register | Makes the two measure types and their data classes known to CARP Mobile Sensing (CAMS). Register before configure(). CarpMobileSensing.ensureInitialized() runs first, because registering needs an initialized CAMS. |
| 2. Protocol | Creates a protocol with the phone as its only device. SQLiteDataEndPoint stores the measurements in a SQLite database on the phone. |
| 3. Measures | Both measures are one-time measures, so the PeriodicTrigger collects them when sampling starts and then once a day. The HistoricSamplingConfiguration makes the first app usage window cover the past day and each later window start where the previous one ended. |
| 4. Deploy | Adds the protocol as a study, deploys it and resumes sampling. The app adds the study only on the first run: on later runs, configure() restores the saved study. |
| 5. Measurements | Prints every measurement as JSON. |
Grant usage access
App usage needs the Usage access setting, which no permission dialog asks for:
Run the app
Both measures collect as soon as sampling starts.
Turn on usage access
When Android opens the Usage access settings screen, select the app and allow access. The first app usage measurement, taken before access was granted, has an empty
usagemap.Restart the app
The trigger then fires again, and both measures collect with access granted.
Check the data
The installed apps arrive as one dk.cachet.carp.apps measurement that lists every app. An app usage measurement looks like this, formatted for readability:
In the JSON, each app's usage is in microseconds: 2712000000 is 45 minutes and 12 seconds.
To use the data in the app, check the type of measurement.data:
Next steps
Installed apps
The apps installed on an Android phone, with version and update time.
App usage
Foreground time per app over a time window, from Android usage statistics.