辛格尔顿或者服务的套接字连接 [英] Singleton or Service for socket connection

查看:110
本文介绍了辛格尔顿或者服务的套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,我试图端口iOS应用程序到Android。基本上我的应用程序需要与插座IO实时的服务器进行通信。它连接到服务器,然后处理各种信息,直到服务器完成处理请求。

I'm new to Android development, i'm trying to port an IOS app to Android. Basically my App need to communicate with a server in real time with socket IO. It connect to the server and then handle the various message until the server finish to process the request.

在我的iOS应用程序,我使用Singleton模式的请求发送到我的WebSocket服务器,我用的是同一个实例委派服务器响应。
在Android中我会使用相同的模式,用一个回调对象在我的活动得到一个服务器响应后,将用户重定向。

In my IOS app i use a Singleton pattern to send the requests to my websocket server, and i use the same instance to delegate the server response. In Android i was going to use the same pattern, with a callback object in my activity to redirect the user after getting a server response.

我需要我的应用程序,以保持套接字连接打开,直到我们得到了来自服务器的权限的状态,即使该应用程序进入后台。

I need my app to keep the socket connection open until we got the right status from the server, even if the app goes in background.

有人建议使用服务与广播接收器,而不是单例。它是在我的情况做的最好的事情?

Some people recommend using Service with Broadcast receiver instead of Singleton. Is it the best thing to do in my case ?

推荐答案

使用服务正是我已经做了非常类似用途(做蓝牙socket通信和TCP / IP的应用程序),并认为你肯定会想使用一个服务如果通信继续即使在用户关闭应用程序。

Using a Service is exactly what I have done for very similar purposes (doing socket communication for Bluetooth and TCP/IP applications) and think you'll certainly want to be using a Service if the communication should continue even when the user has closed the application.

A 服务基本上运行在UI线程上code的手段(当然你就可以开始在它的其他线程),但没有一个用户接口,不像一个活动有与它相关的UI。

A Service is essentially a means to run code on the UI thread (but of course you can then start off other threads within it) but without a user interface, unlike an Activity which has a UI associated with it.

如果你尝试,你提出来替代这样做是一个静态的单例类的话,我觉得问题是,你不会有比它的生命周期中很好的控制。如果用户导航离开应用程序,那么我的理解是,它是由框架时,它选择删除过程和所有与它一起的静态对象。出于这个原因,如果你有填充数据单类和退出应用程序,再后来回来的应用程序,你可能会或可能不会发现,老单实例仍然存在。出于这个原因,我的应用程序(它使用了非常大量的全局状态)我已经使出抱着我单身的实际情况在。应用类的扩展,(希望)更好地控制自己的生命周期。

If you were to try to do this in a static singleton class as you propose as an alternative, then I think the problem would be that you wouldn't have very good control over the lifecycle of it. If the user navigates away from the application, then my understanding is that it's up to the framework when it chooses to remove the process and all the static objects along with it. For this reason, if you have singleton classes populated with data and you exit your application and then later come back to the application, you may or may not find that the 'old' singleton instances are still around. For this reason, in my application (which uses a very large amount of global state) I've resorted to holding my singletons' actual instances in an extension of the .Application class, to (hopefully) better control their lifecycle.

随着服务你有合适的生命周期回调(的onCreate()一个明确定义的生命周期的onDestroy()等),就像你用活动做

With a Service you have a well-defined lifecycle with appropriate lifecycle callbacks (onCreate(), onDestroy(), etc.) just as you do with an Activity.

这篇关于辛格尔顿或者服务的套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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