I need to create an activity that starts a service that monitors the users position and when within a certain zone allow the calling Activity to update its view informing the user.
Since there are a few different ways to work with services I am a little confused to which of these is the correct one for my situation.
Use the startService() and stopService() methods: From what I understand I can’t directly talk back to the starting activity. In the google docs there is an example showing how to pass a BroadcastReceiver to the service via a PendingIntent and call it, but I don’t think that allow for me to update the running activity’s view…or will it?
Bound Service: From the docs it seems that this will allow for 2-way communication between the service and activity, but it was also mentioned that bound services don’t run in the background indefinitely. Now I don’t need, or even want, the service to run indefinitely, but in the worst case scenario I could need it to run in the bg for at least an hour or two without being killed.
Third (probably not so great) option: Run the location service in a thread in the activity, which will make real-time view updates easy, then in the onPause event, stop the location service in the activity and hand it off to a service via startService() and use the notification service to alert the user when a defined zone has been entered.
Any advice would be greatly appreciated.
-
this says
if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.
-
and this says
A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
Ah, option (2) is probably what you want. You can override
onStartCommandto returnSTART_STICKYand the Service will continue to run after the Activity finishes.When you
bindServiceyou can get the actual Service instance back and then just invoke your custom methods on that as a way of communicating. Alternatively you could use one of the examples here