如何在 Android 中与正在运行的线程进行服务通信
我的目标是推出一项能够满足所有应用程序网络需求的服务。
我想也许打开2个套接字用于数据传输。我希望异步处理数据,所以我想我应该在两个单独的线程中运行它们,每个线程针对每个套接字,这样数据就可以在两个不同的“链接”异步中进行流式传输。
所以,我会欣赏两件事:
更好的整体设计。也许我完全弄错了..
有人可以向我解释一下,一旦我需要将数据传递给主服务或从主服务传递数据,我如何与这些线程进行通信?据我所知(操作系统),我需要使用信号:)(只是开玩笑..)
my goal is to lunch a service that will take care for all of the application network needs.
i thought maybe to open 2 sockets for data transfer. i want the data to be handled asynchronously, so i was thinking my be i should run them in two separated threads, each for every socket, and that way the data could be streamed in two different "links" async..
so, i would appreciate two things:
a better overall design. maybe i completely got it all wrong..
can someone explain to me how can i communicate with those threds once i need to pass data to/from them to/from the main service? as far as i learnt (OS) i need to use SIGNALS:) (just kidding..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我可以说我终于明白了,就在放弃之前。这是一个超级简单的应用程序,它在活动内运行一个线程,并为每个实体使用两个不同的处理程序处理双向通信!
代码:
输出是:
我在阅读 endian 提供的帖子时发现了这一点, 这里。
我希望其他人会发现这很有用。祝你好运:)
Well, I can say i finally got it, just before giving up. Here is a super duper extremely simple app that runs a thread inside an activity and handles a bi-directional communication using two different Handlers for each entity!
The code:
the output is:
I figured this out while reading the offered post by endian, here.
I hope others will find this useful. good luck:)
这里是很好的帖子,解释了线程和通信处理程序。此外,同一博客还有许多关于 Android 中各种线程构造的帖子。
另一种可能性是使用 AsyncTasks。 此处查找一些说明
Here is good post explaining threads and communication using handlers. Also, the same blog has a number of posts regarding various thread constructs in Android.
Another possibility is to use AsyncTasks. Find some explanation here