如何在不同进程的Activity和Service之间建立双向通信? [英] How to establish a two-way communication between Activity and Service in different process?

查看:20
本文介绍了如何在不同进程的Activity和Service之间建立双向通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在 Activity 和在不同进程中运行的服务之间建立双向通信.

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

从Activity中查询进程没什么大不了的.但我希望该过程能够在事件上通知 Activity.其背后的想法是:服务独立于实际应用程序运行.它定期查询网络服务器.如果在网络服务器上发现新任务,该进程应通知活动.

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.我已经实现了一个应该通知 Activity 的接口,但问题是监听器始终为 null,因为来自进程的广播是通过 Intent 完成的,因此扩展 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,

工作人员

推荐答案

要么使用 BroadcastReceiver 或有 Activity 注册一个回调或侦听器对象Service 调用关键事件.上面的链接是用于展示每种技术的示例项目.

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.

这篇关于如何在不同进程的Activity和Service之间建立双向通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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