I have an Activity that starts a service. When the activity is closed, I want the service to continue running in the background. I have a couple of questions here.
Will closing the activity screen cause the activity to actually stop? Or do I need to forcibly stop it to cause it to stop?
If closing the screen does cause it to stop, then I assume I need to use startService to start it. Is that correct? If that is the case, is there a way to get a handle to the running service next time the activity starts? If it is not the case, then I can just bind to the service.
When your
Activityis no longer visible on the screen, it is stopped. Stopping anActivitybound to aServicedoes not stop theService. However, you’ll want to make sure you unbind from theServicewhen yourActivitycallsonDestroy()to make sure you don’t have any dangling handlers and suchlike.You can rebind to a running
Servicethe same way as you did the first time. Sending anIntentto start aServicethat’s already running doesn’t create a second instance of it, so that’s safe.A good way to start a
Servicelike you’re describing is to start it using theContext.BIND_AUTO_CREATEargument to your call tobindService().The details of all of this can be found at the Android docs about bound services. It can be a little confusing at first. Follow the tutorial code closely since it sounds like your problem maps well onto the sample they provide.