Android – need service to reply to activity

I need to create an activity to start the service that monitors the user's location, and allow the calling activity to update its view in a certain area to notify the user

Since there are several different ways to deal with services, I'm a little confused about which is right for my situation

Using startservice () and stopservice () methods: as far as I understand, I can't go back to the starting activity directly. There is an example in Google Docs that shows how to pass the broadcastreceiver to the service through pendingintent and call it, but I don't think I'm allowed to update the view of the running activity... Or will it?

Binding service: according to the document, this will allow two-way communication between the service and the activity, but it is also mentioned that the binding service does not run indefinitely in the background. Now I don't need, or even want, the service to run indefinitely, but in the worst case, I may need it to run in BG for at least one or two hours without being killed

The third (maybe not so good) option: run the location service in the active thread, which will make it easy to update the real-time view. Then, in the onpause event, stop the active location service and give it to the service. Remind the user when entering the defined area through startservice() and use the notification service

Any suggestions would be appreciated

>This says

>Talk to this

resolvent:

Ah, option (2) may be what you want. You can override onstartcommand to return to start_ Sticky, and the service will continue to run after the activity is completed

When you use bindservice, you can get the actual service instance, and then just call your custom method as a communication method. Alternatively, you can use one of the examples here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>