Communication between Service and Activity on Andr

2020-02-26 02:36发布

问题:

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 BroadcastRecevier.

Which are my other options? Does anyone have a tutorial/reference regarding this?

Answer to this question is:

There are several way for an activity to communicate with an service and vice versa. This section dicusses the different ways and gives recommendation which to use.

5.1. Activity binding to local service If the Service is started in the same process as the Activity, the Activity can directly bind to the service. This is a relatively simple and efficient way to communication.

5.2. Using receiver You can also use dynamically registered receivers for the communication. For example your activity can dynamically register a receiver and the service sends outs corresponding events.

5.3. AIDL for services in a different process To bind to a service which runs in a different process you need to use Inter Process Communication (IPC) as the data needs to be send between different processes. For this you need to create a AIDL file which looks similar to an Java interface but ends with the .aidl file extension and is only allowed to extend other AIDL files.

This approach is required if your service should be provided to other applications, otherwise you should prefer a local service.

5.4. Sending Intent data and bundle to the services The service receives data from the starting Android component and can use this data.

5.5. Handler and Messenger If the service should be communicating back to the activity it can receive an object of type Messenger via the Intent data it receives from the Activity. If the Messenger is bound to a Handler in the activity the service can send objects of type Message to the activity.

A Messenger is parcelable, which means it can be passed to another process and you can use this object to send Messages to the Handler in the activity.

Messenger provides also the method getBinder() which allows to pass a Messenger to the activity. The Activity can therefore send Messages to the service.

Thanks to http://www.vogella.com/articles/AndroidServices/article.html

回答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.



回答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.