I want to develop an Android application that satisfies the following specifications:
- Record data from a sensor (for example the accelerometer) at an approximate rate of 10-30 Hz.
- Upload this raw data to a remote server (for example using TCP sockets).
- The user interface should be minimum, just a pair of buttons to start or stop the recording and transmission of the data.
- All the process should be unnoticeable for the user and keep working when the screen goes off for several hours.
- Battery life is not critical (it should last several hours).
Vision: I would like to analyse in quasi-real time the sensor measurements of a group of users without their intervention (apart from starting the application).
After some research, I could manage to perform these tasks separately and inefficiently. I've tried classes such as Service and IntentService, SensorEventListener, etc. I don't know if I should use Thread, Service or IntentService for each task. Specifically, I have serious problems to communicate them.
My questions:
- What class(es) do you recommend to use in order to solve this problem?
- What task should be done on each of them?
- If the tasks are performed in different entities (threads, services, intentservices, etc.), how should I intercommunicate them (I'm thinking about the recording and uploading tasks)?
I am asking for the best-practice structure to solve my problem. You do not need to go into details in terms of developing/coding the solution.
Thank you very much and feel free to ask if something is not clear enough.
David
UPDATE:
After some more research and thanks to DROIDcoder, I manage to design a skeleton for my app:
- Main UI: Activity object presenting two buttons (start/stop) that will launch a Service with the usual startService/stopService methods
- Background: Service object
- Awake when screen goes off: the service requests a power lock in onCreate() and releases the power lock in onDestroy(). Find more info here: https://developer.android.com/training/scheduling/wakelock.html#cpu
- Log sensor values: implement SensorEventListener in the Service as usual
- Upload sensor values to server: use AsyncTask in the service as described here: How to use AsyncTask
- Data formatting and transmission: use GSON library + HttpClient as described here: How to send POST request in JSON using HTTPClient?
- Testing: use the website http://www.jsontest.com/ to test the JSON queries
As this is only a proposition, I add it as an edition. I will post a detailed answer when the whole system works.
The questions remains: can you think about a better design for the application?
Thanks again!