如何从一个活动发送TCP套接字到另一个机器人? [英] How to send TCP Sockets from one Activity to another in android?
问题描述
假设我已经创建了活动A.客户端TCP套接字我想发送的Socket到活动B.现在,在活动B,根据一定的条件,我将关闭套接字。问题是intent.putExtra()亘古不支持发送插槽。那么怎么办呢?
Assume that i have created a client TCP Socket in Activity A. I want to send that Socket to Activity B. Now in Activity B, based on some conditions i will close that socket. The problem is intent.putExtra() doesnot support sending sockets. So how to do that?
推荐答案
如果你认为有多个活动拥有一个插座,然后他们都不应该自己的插座。别的东西,像服务
,应该是负责管理插槽。在这种情况下,活动A可以启动服务
做插座的工作,而当插槽不再需要b活动可以停止该服务。
If you think that there are multiple activities that "own" a socket, then none of them should "own" the socket. Something else, like a Service
, should be in charge of managing the socket. In this case, Activity A could start a Service
to do the socket work, and Activity B can stop the service when the socket is no longer needed.
否则,如果这些活动是紧耦合的,它们不应该是独立的活动。活动的目的是松散耦合,类似于网页在一个Web应用程序。
Otherwise, if those activities are that tightly coupled, they should not be separate activities. Activities are designed to be loosely coupled, akin to Web pages in a Web app.
这篇关于如何从一个活动发送TCP套接字到另一个机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!