0

I have a service with a content observer watching the calendar provider and there are some instances where i need to stop the service so the observer's onchange does not get called when adding an event and then restart the service when it is added.

In my activity I use stopService(new Intent(context,Service.class)); to stop the service then I start it up again later on trying to avoid the onchange call when the provider gets changed but it does not appear the service gets stopped because the onchange still gets called causing force closes in my database because my query in my service cant find the event yet.

so how can i get around this problem?

tyczj
  • 71,600
  • 54
  • 194
  • 296

2 Answers2

0

If you are returning START_REDELIVER_INTENT that might impact you. Here is what the docs say about it:

if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).

But, then again, that says "killed", so it shouldn't impact you.

Other than that, I'm not sure what the problem would be, but if you really can't get it to work and noone else gives you a better answer (which hopefully someone will), you could do registerReceiver(...) in the service, and then in your Activity do sendBroadcast(...), and in the onReceive in the services's receiver, call stopSelf(). I know it's sloppy, and not the best approach, but it's an idea for an alternate method.

Reed
  • 14,703
  • 8
  • 66
  • 110
0

Try like this:

ComponentName service = startService(new Intent(this, BaseballWatch.class));
// Stop a service using the service name.
stopService(new Intent(this, service.getClass()));
// Stop a service explicitly.
try {
  Class serviceClass = Class.forName(service.getClassName());
  stopService(new Intent(this, serviceClass));
} catch (ClassNotFoundException e) {}
jainal
  • 2,973
  • 2
  • 20
  • 18