I am developing an app that the main objective is collect data from the sensors in the background and send it to a DataBase. To do so, I am using a Remote Service
for taking the data from the background.
Inside the Service
, I have a periodic TimerTask
in a Timer
, that does:
I decide to start and stop the sensors every time because it is done every 30 sec, and it's better than have them set. Moreover, I have the same problem if I set them before and add a new one that wasn't set, and setting all them is not efficient as the GPS drains a lot the battery.
The problem is that PhoneStateListener
and LocationManager
don't work inside a non looper thread (Timer
is a non looper thread).
Is there any way to solve this problem? Is there any trick to skip it?
Finally, what I did was using a Runnable inside a Service, and using a ScheduledThreadPoolExecution
, as the following way:
ScheduledThreadPoolExecutor execUpdateData = new ScheduledThreadPoolExecutor(1);
scheduledUpdateData = execUpdateData.scheduleAtFixedRate(new updateDataTask(), 0, sampling_storage_ms, TimeUnit.MILLISECONDS);