4

I have a foreground service in which I register for location updates, and intent filter for the battery intent Intent.ACTION_BATTERY_CHANGED, and a LocalBroadcast. All of these I remove in the onDestroy of my service as once this service is dead.

I also have changed the return integer in the onStartCommand from START_STICKY to START_NOT_STICKY but this has no effect.

I log when this service is started and destroyed as well as my main activity and it seems that this service just starts on its own without any activities.

The issue is it's a foreground service so a notification accompanies it. This means that when it's started up again randomly, the user sees the notification and I don't want the service to be running on its own accord.

So to recap.
- It is a bound, foreground service.
- I return START_NOT_STICKY in onStartCommand
- When I no longer need it and close the app, I unbind from it, call stopService and the service's onDestroy is called which is where we unregister all of the receivers. - The service is randomly started after this.

StuStirling
  • 15,601
  • 23
  • 93
  • 150
  • How is the Service declared in the manifest? Do you have any Intent filters specified for the service there? – mach Feb 25 '15 at 06:38
  • Sorry should have mentioned that. No, I have no intent filters setup in the manifest. Literally just the declaration – StuStirling Feb 25 '15 at 06:40
  • 1
    The code for the Service would be nice to have. Have you tried logging the onBind()- and onNewIntent()-functions in the service to try to determine if it is started due to an activity binding to it or if it is an incoming Intent? – mach Feb 25 '15 at 07:01
  • I put those logs in last night so will wait for it to occur again. I think this could be a Lollipop issue as didn't see before that and don't have any reports of it on other devices. Service is too big to post code – StuStirling Feb 25 '15 at 07:04
  • This is usually because there's a background thread somewhere that calls the service back from the dead. I have this sort of problem and having been trying to come up with a better synchronized way to make sure the background thread dies before the service does. – TheRealChx101 Jul 19 '19 at 16:11
  • Well i am still facing the same issue with service as you but not with all devices actually it's happening randomly .. not sure why but i think the service is recovering randomly intent from somewhere .. if you figured out could you share – Bahaa Odeh Apr 26 '20 at 21:58

1 Answers1

3

Since you use a foreground service, you should call stopForeground method "Caution: The integer ID you give to startForeground() must not be 0."

There are only two ways to start a service, either binding or start it from a context, you must make sure you aren't doing it inside your code.

Maybe you should check on the service lifecycle: http://developer.android.com/images/service_lifecycle.png

reinaldomoreira
  • 5,388
  • 3
  • 14
  • 29