I need to create an action that starts a service that monitors the user's position, and when in a certain zone it allows the caller to update their view, informing the user.
Since there are several different ways to work with services, I am a little confused which one is right for my situation.
Use methods startService()andstopService() . From what I understand, I can’t directly return to the starting activity. There is an example in google docs showing how to pass BroadcastReceiver to a service via PendingIntent and call it, but I don’t think let me update the view of the current activity ... or will it?
Linked service . From the docs, it seems this will allow for a two-way connection between the service and activity, but it was also mentioned that related services are not running in the background indefinitely . Now I don’t need or even don’t want the service to work indefinitely, but in the worst case I might need it to run in bg for at least an hour or two without being killed.
The third (maybe not so big) option . Run the location service in the stream in an activity that will facilitate real-time updating, and then in the onPause event, stop the location service in action and transfer it to the service through startService()and use the notification service to alert the user when a certain zone is entered.
Any advice is appreciated.
it says
if you want the service to send the result back, the client that starts the service can create a PendingIntent for broadcast (using getBroadcast ()) and deliver it to the service in the intent that starts the service. The service can then use the broadcast to deliver the result.
and it says
A linked service usually lives only when it serves another component of the application and does not work in the background indefinitely.
source
share