如何建立活动和服务在不同的进程之间的双向沟通​​?双向、进程、和服务、不同

2023-09-12 04:26:40 作者:你是今生的唯一

我工作的一个活动,它运行在一个不同的进程服务之间建立双向通信。

I'm working on establishing a two-way communication between an Activity and a Service which runs in a different process.

从活动查询的过程也没什么大不了的。但我想这个过程就事件通知活动。其背后的想法是这样的:该服务独立地从实际的应用程序运行。它定期查询的网络服务器。如果一个新的任务在网络服务器中发现的过程应通知活动。

Querying the process from the Activity is no big deal. But I want the process to notify the Activity on events. The idea behind it is this: the service runs independently from the actual app. It queries a webserver periodically. If a new task is found on the webserver the process should notify the activity.

我发现这个线程的结束,在AndDev.org,但它似乎并没有为我工作。我一直在瞎搞与BroadcastReceiver的。我实现了其应通知活动的接口,但问题是,听者总是空,因为从工艺的广播是通过意向做,因此扩展的BroadcastReceiver类将重新初始化。

I found this thread over at AndDev.org but it doesn't seem to work for me. I've been messing around with BroadcastReceiver. I've implemented an interface which should notify the Activity but the problem is that the listener is always null since the Broadcast from the process is done via Intent, hence the class that extends BroadcastReceiver will be newly instantiated.

我如何建立一个双向的沟通?这必须是可能的。 感谢您的帮助,

How can I establish a 2-way communication? This has to be possible. Thanks for any help,

steff

推荐答案

或者使用的 的BroadcastReceiver 或拥有的 活动注册一个回调或侦听器对象了的关键事件服务 要求。上面的链接是预定范例项目展示各自的这些技术。

Either use BroadcastReceiver or have the Activity register a callback or listener object that the Service calls on key events. The links above are to book example projects demonstrating each of those techniques.