服务和活动在Android之间的通信 [英] Communication between Service and Activity on Android

查看:120
本文介绍了服务和活动在Android之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个活动,在Android的服务之间进行通信的方式?

What are the ways to communicate between an Activity and a Service on android?

今天,我学会了如何通过活动发送意图及回复使用BroadcastRecevier沟通。

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?

这个问题的答案是:

有几种方式为活动,以与服务进行通信,反之亦然。本节兼谈不同的方法,并给出建议,该建议使用。

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。活动结合本地服务 如果服务被启动,在相同的处理的活动,活动可以直接绑定到服务。这是一个相对简单的和有效的方式来进行通信。

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。使用接收器 您还可以使用动态注册接收器的通信。例如你的活动可以动态地注册一个接收器和服务发送相应的事件出局。

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在不同的进程服务 要绑定到它运行在您需要使用进程间通信(IPC)的数据需要发送不同进程之间的不同进程的服务。为此,需要创建看起来类似于Java接口但与.aidl文件扩展名结尾并只被允许扩展其他AIDL文件AIDL文件

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.

如果您的服务应提供给其他应用程序,则需要此方法,否则你应该preFER本地服务。

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

5.4。发送意图数据和捆绑的服务 服务接收从起始机器人组件数据,并可以使用此数据

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。处理程序和Messenger 如果服务应连通回活动它可以通过它接收到来自活动的意图数据接收类型信使的对象。如果Messenger被绑定到一个处理器中的活动的服务可以发送类型的消息对象的活动。

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.

一个Messenger是parcelable,这意味着它可以传递到另一个过程中,你可以使用这个对象发送消息的处理程序中的活动。

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.

通还提供了方法getBinder(),它允许传递使者的活动。因此,该活动可以将消息发送到服务。

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

由于 http://www.vogella.com/articles/AndroidServices/article。 HTML

推荐答案

方法来活动连接到服务:

Ways to connect Activity to service:

  1. 广播:最简单的方式,实现 BroadcastReciever 在每个听取别人的行为

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

使者:多种类型的客户端非常好,无论是服务和客户端有一个使者,服务提供是信使 onBind( ),客户端发送注册/注销的信息与自己的使者的replyTo消息()。服务节省了客户的使者。现在,这两个可以发送/接受她的消息。

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.

的IBinder:如果您需要完全成熟的远程IPC。定义一个接口与AIDL服务,并在 onBind实现传递给客户()

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

Android的在线参考都有各自的解释。

Android online reference has explanations of each.

这篇关于服务和活动在Android之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆