How to pausing and resume a service

Dan picture Dan · Jan 14, 2013 · Viewed 7.6k times · Source

I'm an android beginner, and unfortunately I could not find a satisfying answer for my service problem so far. I hope you can help me. I think that my question addresses a common task.

My situation is the following: I have implemented my own service that simply records GPS locations and stores them in a list. Once the service stops, it writes the data to a database.

This works well, but now the service should have some kind of "pause mode" where it stops recording and can be resumed later on. What would be the best way to implement this functionality?

So far I came up with two (admittedly not very satisfactory) ideas:

  1. Implementing my own service lifecycle (start, stop, pause, resume) and binding the GUI to the service. Moreover, it seems that the binding happens asynchronously, which makes updating the GUI (enabling and disabling the start/stop controls) a little messy.

  2. On pause, the service is stopped and the GPS locations are appended to some list in a singleton class. On resume, the service is started again and so on. The problem I see here is that the application might get killed while in pause mode and the data is lost.

Answer