How to get data from an already running service?

I have a service that starts after the application starts, and I was interested to know how I can get data from this service for activity at any time without restarting the service.

+6
source share
5 answers

Do not be offended, but the “messenger” and the “binding” method made me take about two days to understand. Not that it was wrong, but these are too complicated things for me.

In my opinion, it’s easier for a service to translate data so that Activity can simply access the data when it is available. Do a lesson at this link: http://www.websmithing.com/2011/02/01/how-to-update-the-ui-in-an-android-activity-using-data-from-a-background- service / comment-page-1 / # comment-734

(took me 10 minutes to understand and implement ... a great tutorial)

+5
source

If the service is already running, the startService() call does not restart the service. It simply sends a command that will be received by the service in onStartCommand() . Thus, you can send a command that starts the service to do something with the name of the activity, which may include messaging from the service back into action (for example, via Messenger ).

Or, as @Tom Dignan points out, you can get attached to the service. Just be a little careful as you are dealing with configuration changes, for example when the user rotates the screen.

+4
source

For this you bind the service. Extensive information is contained in Android docs: http://developer.android.com/guide/topics/fundamentals/bound-services.html

+1
source

Currently (end of 2013) there are better solutions for getting a link to a working service in Android, just use a library, for example Otto or EventBus .

Add eventbus to your activity and service, implement the onEvent() method on both sides, create one or two classes xyzEvent ( xyz = the name of your event), register references to the activity and service in eventbus ('this').

Send an event to the service from your activity on eventbus, catch it in the onEvent method of your service, publish a new event in response to the activity, catch it in the onEvent method of your activity: now you have a help desk ( this service) available as part of this operation .

Everything is set up and done without any fuss. Activity and maintenance are fairly untied and easy to implement. There is nothing special about this. No direct binding, no messengers, no AIDL. Only the old-fashioned K.I.S. principle.

Now you have a link to your service from your activity and you can call any direct method available in the service.

+1
source

To get data from the current service to the action (for example, at the click of a button), you must use the Bounded Service, you become attached to the service, get a link to it and call the public method. more details here and here

To send data from a service to action, you can use Handlers / Messenger using streams or broadcasts of intentions and broadcast receivers.

+1
source

Source: https://habr.com/ru/post/888720/


All Articles