如何处理活动的生命周期涉及到插座中的Andr​​oid? [英] How to handle activity life cycle involving sockets in Android?

查看:253
本文介绍了如何处理活动的生命周期涉及到插座中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 在我这又启动一个线程一个Android的活动。
  • 在该线程我打开一个持久的TCP套接字连接。
  • 当插座连接到服务器的动态数据被下载。
  • 的线程发送用处理程序级至当已经接收数据的活动的消息。

现在,如果用户恰好从纵向切换到横向模式的活动得到了的onDestroy调用。这时我关闭套接字,并停止线程。

Now if the user happens to switch from portrait to landscape mode the activity gets an onDestroy call. At this moment I close the socket and stop the thread.

当Android已经切换横屏模式,它调用的onCreate又一次,我要做一个插座重新连接。此外,所有的活动接收需求数据被下载一次,因为服务器不必知道什么已发送之前,即没有恢复功能的能力。

When Android has switched landscape mode it calls onCreate yet again and I have to do a socket re-connect. Also, all of the data the activity received needs to be downloaded once more because the server does not have the ability to know what has been sent before, i.e. there is no "resume" feature.

因此​​,问题是,有很多数据是怨恨都在风景模式被改变的时间。

Thus the problem is that there is alot of data which is resent all the time when landscape mode is changed.

什么是我选择这里?

  • 我应该创建一个处理向服务器套接字通信服务因此,我总是把所有的服务器中的服务发送的数据。
  • 或者我应该禁用风景模式一起吧?
  • 或者将我最好的选择是重写我的服务器,它是一个非常大的工作: - )

所有输入欢迎: - )

All input is welcome :-)

/亨里克

推荐答案

无论是prevent被重新启动你的活动(<一href="http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android">http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android)或实现您的应用程序管理服务,该服务方法与应用我所看到的非常流行,并且允许你一定的灵活性撕裂下来的时候连接。

Either prevent your activity from being restarted (http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android) or implement a Service which your application manages. The Service method is very popular with applications I have seen, and allows you some flexibility when tearing down the connection.

这篇关于如何处理活动的生命周期涉及到插座中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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