I'm developing an application for a small research project that utilizes the ActivityRecognition
API and the GPS. What I'm trying to achieve is to have both APIs running as a background process, and record current data periodically in a log file. This log file can then be manually analysed after data is collected.
My trouble is understanding the best way of going this. I originally developed from the ActivityRecognition sample app. My process is as follows:
- Create location listener via requestLocationUpdated when button in MainActivity is tapped. Also create ActivityRecogition listener in MainAcitivity.
- When location update event is triggered, sending data via broadcast.
- If MainActivity is running, listen to broadcast via broadcastReciever, update gui, store last update in class-wide scope variable (in MainActivity)
- When ActivityRecognition gets an update, log the data in a file along with the GPS data stored in class-wide scope variable in MainActivity.
The obvious problem with this is that although the location service operates in the background even when the MainActivity has hit an onPause, the ActivityRecognition nor the file logger are not running in the background as a service so require MainActivity to be running.
What's the best practice for handling multiple background processes for one purpose? To create on background service to handle both of these? Then run this on app onCreate/button tap? Then send data to MainActivity (if it's there to listen) via a broadcast message?