Question

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:

  1. Check sensors setting
  2. Activate sensors which data is wanted
  3. Take the data
  4. Stop sensors

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?

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top