From my little android knowledge I understand that android OS can kill my service under extreme memory conditions.
I have created a service that returns START_STICKY. The service is meant to run in background.
If android is about to kill my service, will it call onDestroy ?
And when it restarts it would it call onCreate ?
See here, the dev guide. http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
onCreate() is only called when the process starts, which can either be the first time the service is running, or if it was killed on restarted, essentially this is called whenever it starts.
onStartCommand() is called whenever a client calls startService().
When a service is destroyed / completely stopped, Android is supposed to call onDestroy() on that service. I think it's possible for that to not happen (e.g. process is killed not through Android system). In the case of a bound service, this is when there are not more active client binders.
Edit: onCreate() Service starts; onStartCommand()someone uses service; onDestroy()Service is killed / stopped.
If someone calls Context.startService() then the system will retrieve
the service (creating it and calling its onCreate() method if needed)
and then call its onStartCommand(Intent, int, int) method with the
arguments supplied by the client
...
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
http://developer.android.com/reference/android/app/Service.html
EDIT: Quick answer. Yes to both questions
Related
I'm trying a simple android app that starts and stops a simple service with startService(). I notice that each time I manually terminate the app process (by closing the app), another instance of my service is created.
In other words, the service that I created isn't destroyed, and an additional service is created. I can see that the onDestroy() isn't called, and the onCreate() & onStart() get called again
Why is that? How can I prevent it? You can see the source code I'm using in this example: Source Code
If you start your Service using Context.startService() and your Service returns START_STICKY from its onStartCommand() then the system is expecting the Service to either be explicitly stopped via something calling Context.stopService() or the Service itself calling stopSelf(). When you use the Recents screen to "swipe away" your app, you are forcefully killing the process but not explicitly stopping the Service.
I have 2 questions regarding the onDestroy() method of a service
1.Will the onDestroy() method of a service be called if the service is killed by the system because of lack of memory.
2.Will the onDestroy() method of a service be called if the service is killed because the app that started the service was swiped away by the user in the resent apps screen.
When the service is terminated by system onDestroy will be called, but when your application process is terminated onDestroy may not be called.
onDestroy() will be called IF service is being terminated as a result. (this is not always the case) See this discussion for more info.
First question.
Based on my previous experience and Android dev docs.onDestroy() method of a service is called even if the service is killed by the system.Google Android dev documentation is belowe.
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service's onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
Second question.
If you would like to kill service when you close your app so yes. Aneroid services are running on the background and normally are not killed when you destroy you're app.
Android provides the Service class, which can be useful for background or non-UI operations.
I have a question about Services' lifecycle.
I know that bound services have the lifecycle like following:
Some component starts the Service via bindService() -> onCreate()
onBind()
process
The binding component calls unbindService() -> onUnbind()
onDestroy()
My question is:
Activities usually call unbindService() at onStop().
However, the Activity can be killed without calling onStop() - I mean, when the system memory is low, the only method that must be called is onPause(). onStop() is after onPause(). Before calling onStop(), the Activity can be destroyed.
In this case, the Service didn't get unbindService(), so the Service is still running. Is this right?
Of course, this rarely happens because Services are background by default. (Services are more likely to be killed by system on low memory.) However, a "Foreground" Service has higher priority than the "onPause()ed activity." according to http://developer.android.com/guide/components/processes-and-threads.html . In this case, the binding activity will be killed first.
If this thing happens, the Service does not end? If memory is not low anymore, then the Activity will be created again, but will call bindService() again since it is a new instance. Also, the Activity even may not restart. Isn't this right? What can I do in this case?
The Service is killed, but if you have 'return START_STICKY' being returned from the onStartCommand(...) [AND you are starting the service using 'startService(intent)'], the service will start back up again. The Service will start back up even if the Activity is not opened again.
I have run this example - the BoundedAudioService example and tested by killing the activity - the service restarts itself. (By restart I mean, the onStartCommand(...) of the service is called again)
A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
I execute stopSelf as service finishes working (from within service) but it is not destroyed (see using log output in onDestroy()) right after it. It leads that startService uses existing (stopped) service instead of starting new one. How can i force service to be destroyed right after it is stopped?
From Documentation:
A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these
situations hold, the service's onDestroy() method is called and
the service is effectively terminated.
I have a question related to how Android service behaves when stopped by system. As per my understanding, if system (OS) stops the service due to resources needed, it is responsibility of system to start it again. In this scenario, system would not call onDestroy() when stopping service and onCreate() when starting service, rather it would just call onStartCommand().
If I am creating a thread in onStartCommand(), how would I cleanup thread when system stops service. If I dont stop thread, onStartCommand() would create a new thread.
I think, it may come down to onStartCommand() parameters (intent, flags and startId). They might be different when system starts service after stopping it because it needed resources. Could anyone tell me what would be difference in parameters when service started by startService() command or by system itself (after stopping it)
I can create thread in onCreate() but I am not sure if thread still exists when system stops service. What would be the best way to handle this kind of scenario.
Thanks
As per my understanding, if system (OS) stops the service due to resources needed, it is responsibility of system to start it again.
That depends upon what you return from onStartCommand(). START_NOT_STICKY, for example, means the OS does not have to start your service again.
In this scenario, system would not call onDestroy() when stopping service and onCreate() when starting service, rather it would just call onStartCommand().
Whether it calls onDestroy() would depend a bit on how the service is stopped (e.g., directly or via process termination). However, it should still call onCreate() on the new instance, if and when the OS restarts the service.
If I am creating a thread in onStartCommand(), how would I cleanup thread when system stops service.
Ensure that in onDestroy(), something happens that will cause the thread to go away. Either onDestroy() will be called (and your service can do its cleanup), or your process is being terminated (and your thread goes away with it).
Could anyone tell me what would be difference in parameters when service started by startService() command or by system itself (after stopping it)
There is a START_FLAG_REDELIVERY that will be in the flags passed to onStartCommand(), but AFAIK it will only be set if you return START_REDELIVER_INTENT from onStartCommand().