How to pause and resume service

I am a beginner android, and, unfortunately, have not yet found a satisfactory answer to my problem with the service. I hope you can help me. I think my question is about a common task.

My situation is this: I implemented my own service, which simply records GPS locations and stores them in a list. As soon as the service stops, it writes data to the database.

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

So far, I have come up with two (though not very satisfactory) ideas:

  • Implementing my own service lifecycle (starting, stopping, pausing, resuming) and binding the GUI to the service. Moreover, it seems that the binding is asynchronous, which makes updating the GUI (enabling and disabling the start / stop controls) a bit messy.

  • In pause mode, the service stops, and GPS locations are added to a list in a singleton class. When the service resumes, it starts up again and so on. The problem that I see here is that the application may be killed during a pause and data will be lost.

+6
source share

All Articles