Skip to content Skip to sidebar Skip to footer

Communication Between Service And Activity On Android

What are the ways to communicate between an Activity and a Service on android? Today I learnt how to communicate by sending an Intent from Activity and replying back using Broadcas

Solution 1:

Ways to connect Activity to service:

  1. Broadcasts: easiest way, implement a BroadcastReciever in each to listen to actions of others.

  2. Messengers: Very good for multiple types of clients, Both service and client have a Messenger , service provides it Messenger in onBind(), clients sends a register/unregister message with its own messenger in replyTo() of message. Service saves client messenger. Now both can send/recieve messages.

  3. IBinder: If you need full fledged remote IPC . Define an Interface for service with AIDL and pass Implementations to clients in onBind().

Android online reference has explanations of each.

Solution 2:

The guys are right, you should really google for answers!

However, I recently learned a neat way to send intents to a service. You can simply call startService(myIntent) to send an intent to your service. If the service is not running, it will be started. If the service is running, you have the possibility to react on the new information.

Post a Comment for "Communication Between Service And Activity On Android"