I have just started using services in Android and I have a made a simple service that is polling a server every 20 seconds.
Now how can I get this data from my main activity (when it is active)?
Alternatively the service could send it back do my main activity (but only if its active). I don’t want to wake up my activity.
I have read SDK examples of “Binding” but I can’t find an example how to actually get something from the service. Just how to start and stop the Binding.
From the example. If I have the “mBoundService” object in my activity how do I get my data from the service method called eg. “pollingData()”?
My suggestion would be to send out a broadcast using
sendBroadcast()from the service and then use aBroadcastReceiveras an inner class in your main activity. Depending on your service you can just attach the data to the intent usingputExtras()andgetExtras()Hope this helps!
A practical example:
Then in the class the service is talking to:
Hope this example is about what you are looking for, let me know if you need any more details.