6

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.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
Dan
  • 1,539
  • 12
  • 23
  • 2
    I have a question, why cant you use StartService and StopService logic to pause/resume service. Whenever you want to Pause call the stopservice and add the same logic what you are planning to implement for Pause, since you know when the service has to stop or pause, so you should have better control in OnDestroy to know whetehr stop or pause logic to be executed. Can you please explain a scenarios when it is suppose to resume/pause – Ramesh Sangili Jan 14 '13 at 19:17
  • 3
    Option 2 is correct. You don't pause/resume a service, you start and stop it. If you need to persist its data then you need to do that in the service when it is stopped. – Squonk Jan 14 '13 at 19:18
  • Thank you for the advice to go for option 2. However, wouldn't that mean that I also have to store a flag indicating that the service was paused in case that the application gets killed while pausing? – Dan Jan 14 '13 at 19:57
  • @RameshSangili what argument am i supposed to pass to stop service function. I am in a fragment.I tried this getActivity.getApplicationContext().stopService(Intent) . What should be my intent. – Sagar Devanga Jan 08 '15 at 11:54
  • Thanx the problem is solved i referred to this question : http://stackoverflow.com/questions/3165190/how-to-call-stopservice-method-of-service-class-from-the-calling-activity-clas – Sagar Devanga Jan 08 '15 at 12:03

0 Answers0