I'm a little bit confused. I've read that the biggest benefit of using a started service is that this is independent from the application so, even if the android kills your application, your service is still alive. My questions then are:
1) If my service works in the same process than the application, the service will be alive too?
2) I'm using a BroadcastReceiver to detect the changes in the device's connectivity so, if the application is killed but the service is not, would I still receive that connectivity changes?
3) If I want to get the current connectivity status from the service, could I do that by using the getApplicationContext() or it may returns null? In the second case, how could I get the current status of the network?
4) Similar to the connectivity, I'm sending an event through Otto's bus each time that the application changes its status from foreground to background (on application paused/resumed). So, my service is subscribed to that changes in order to know the current application status. If my application is killed, can I trust in that events? Or they could not be sent?
First, if you use a foreground Service
, it is not very likely to be abruptly killed. Having said that:
If my service works in the same process than the application, the service will be alive too?
Well, no. Your Service
is one component of your application. If it is alive, then so is the application (see also third question). But if you return START_STICKY
or START_REDELIVER_INTENT
from onStartCommand()
, then it will be recreated if terminated by system (not if force-stopped) as soon as possible.
I'm using a BroadcastReceiver to detect the changes in the device's connectivity so, if the application is killed but the service is not, would I still receive that connectivity changes?
For a static BroadcastReceiver
(registered in the Manifest
): that depends on how your application was terminated. If the user force-stopped it, then the receiver will only start working again after the user launched your application once more. In all other cases: a static receiver will be intact, and if the receiver was created and registered dynamically, then one should unregister it when shutting down the Activity
/ Service
in time to avoid memory leaks, so this kind of receivers is not meant to work 24/7 anyway.
If I want to get the current connectivity status from the service, could I do that by using the getApplicationContext() or it may returns null?
That one is easier: Service
, just like Activity
, extends ContextWrapper
. So if it is up and running, you can call getApplicationContext()
.
I'm sending an event through Otto's bus each time that the application changes its status from foreground to background (on application paused/resumed)... If my application is killed, can I trust in that events? Or they could not be sent?
I think here you may be mixing up Application
and Activity
. Because if the Service
needs to be notified, then you're most likely thinking of a visible part of your application and something like the famous incoming telephone call.
In this case, your Activity can notify the Service
either in onPause()
or in onStop()
. Both are guaranteed to be called since Version HONEYCOMB
.
Now I must confess I'm not sure about Otto's EventBus, but one option which would certainly work is having a Service
with START_REDELIVER_INTENT
and notifying the Service by calling startService(myNotifyingIntent)
.
Normally, your application process will not be killed instantly just because the current Activity
is no longer in the foreground. So it is quite likely that the Service
will get any notification you send from onPause()
.