Communication Between Service And Activity On Android
Solution 1:
Ways to connect Activity to service:
Broadcasts: easiest way, implement a
BroadcastReciever
in each to listen to actions of others.Messengers: Very good for multiple types of clients, Both service and client have a
Messenger
, service provides it Messenger inonBind()
, clients sends a register/unregister message with its own messenger inreplyTo()
of message. Service saves client messenger. Now both can send/recieve messages.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"