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.
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
Related
I have tried using START_STICKY on the onStartCommand() method of intent services, I have even tried changing the process name of the service on the manifest.xml file. But none works. Whenever I kill the app, the service also gets destroyed. I want to design a service that gets triggered when the user starts the app for the first time and then keeps on running even after user kills the application. Any help will be useful
In service class implement on destroy method which will trigger a broadcast. In that broadcast receiver class implement on receive method. So whenever service destroyed this broadcast receiver class will execute onreceive method. In that onreceive method start the service again.
Your service must be in the foreground to prevent the system from closing it. See the startForeground method in the Service class.
I handle Service and the return code: START_NOT_STICKY. I do not want to restart the service.
But documentation says "Do not recreate the service, unless there are pending intents to deliver."
Could you give me an example of these pending intents that cause restarting the service?
When you return START_NOT_STICKY, this means the following:
If Android kills the process hosting your Service (which it can pretty much do at any time if it needs the resources or if it thinks your Service isn't doing anything useful), the following happens:
If your process is killed after onStartCommand() is called, but before onStartCommand() has completed, Android will restart your Service and call onStartCommand() again, redelivering the Intent that was being processed when the process was killed
If your process is killed after onStartCommand() has completed, Android will only restart your Service if there are pending Intents for your Service. In this case a pending Intent would exist if any component called startService() for your Service and that call has not yet been completely processed by your Service. This could be the case, for example, if a component called startService() while your Service was dead. Or it could happen if a component called startService() while your Service was still in the onStartCommand() method (processing a previous call to startService()).
I am trying to run a service in the background, despite the lifecycle state of the Activity that creates it. To guarantee it's running, the service (in addition to the services it performs) also has a thread that logs once a second. Finally, it also has logs in onStartCommand and onDestroy.
I'm starting the service with startService() and then I bind to it. My understanding is that this should keep the service running, regardless of what happens to its creator Activity. However, if the Activity is destroyed, the service stops logging. If the Activity is simply paused, there is no problem.
The following is also true:
1) onDestroy is never called on the service
2) I never call stopService or stopSelf after calling startService
I'm trying to figure out why the service is dying (there are no exceptions) or at the very least why it's being paused and no longer logging.
A bound service typically lives only while it serves application component and does not run in the background indefinitely.
http://developer.android.com/guide/components/services.html
http://developer.android.com/guide/components/bound-services.html
Use syncadapter instead of service . if you want to transfer data between server and client .
http://developer.android.com/training/sync-adapters/creating-sync-adapter.html
I have a Service running in the foreground, and an Activity that interacts with it. If the Activity crashes, Android kills the entire process, including the foreground Service and its associated Threads.
However, the ongoing notification provided by the Service does not go away, and upon closer inspection, Android's task manager reveals that the Service itself is still running.
How can I kill the foreground Service in this circumstance?
Have you override onStartCommand method of the Service? What value is it returning? If not, try to override it and return START_NOT_STICKY from it.
START_STICKY: If this service's process is killed while it is started, then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.
START_NOT_STICKY: If this service's process is killed while it is started, and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate.
Not sure, as I have never worked on foreground services, but this might be the reason.
Are you sure the service is not running it its own process...
Also can you confirm whether the service is getting restarted..If its getting restarted-it is because you are returning START_STICKY from onStartCommand()
I have an app that has a client Activity and a long-running Service. The Service may be started by the alarm manager or by the Activity itself.
The alarm intent calls startService(). The Activity calls startService, and then binds itself to the service so it can get information from it.
When I back-button out of the Activity, onDestroy() is called on the Activity, the Activity unbinds from the Service, and onDestroy() is called on the Service.
I was under the impression that if I called startService(), then the Service would stay around until stopService()/stopSelf() was called, no matter what clients have unbound from it.
Am I misunderstanding something?
It partly depends on the SDK version you are using. In this Service Lifecycle reference, it says onStartCommand() must return START_STICKY for the behavior you want. Prior to API level 5, there was no onStartCommand and services were all sticky.