保持线程和连接状态的Andr​​oid应用程序中使用的onSaveInstanceState? [英] Keeping threads and connection state in Android app using onSaveInstanceState?

查看:101
本文介绍了保持线程和连接状态的Andr​​oid应用程序中使用的onSaveInstanceState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了Android的多玩家游戏的应用程序。 一个参与者的充当主机(谁创建的游戏实例的名称)和每个其它参与者被连接到主机使用蓝牙

我的问题是这样的,这台主机在以通信运行的某些线程,并保存所有打开的连接。我读过,我的活动可以被摧毁的临时后来又恢复了,我应该使用的onSaveInstanceState机制的。不过,我说的是一个应用程序,充当服务器的游戏,它具有开放的连接和线程服务于其他客户,会发生什么这些连接和线程,如果操作系统是决定破坏我的活动?他们是否终止?如果是这样,这将是为了正确实施这一建议的模式,我怎能对的onSaveInstanceState捆绑有关联吗?它不是可序列化。这同样适用于螺纹,我需要重新创建并在破坏和活动恢复时消灭它们?如果我认为这一切的状态,一些重presents比赛状态静态类?如果我这样做,然后将操作系统不会破坏我的线程/连接?

我看着自带的SDK样品蓝牙聊天,我所以没有处理的onSaveInstanceState,所以这是很清楚该怎么办。

感谢您!

解决方案
  

会发生什么这些连接和线程,如果操作系统是决定破坏我的活动?

如果您创建了一个活动的线程,你最好停止那些线程,否则你会泄漏他们。

  

我怎能对的onSaveInstanceState束的连接?

您不能。

  

这同样适用于螺纹,我需要重新创建并在破坏和活动恢复时?消灭他们。

如果他们是由活动的开始,那么,是的,请。

  

如果我认为这一切的状态,一些重presents比赛状态静态类?如果我这样做,然后将操作系统不会破坏我的线程/连接?

请把你的服务器逻辑(线程,连接,服务器端游戏逻辑)在Android的服务。请使用 startForeground()来保持这种服务去,把一个通知在状态栏中沿途所以用户有一个简单的方法来找回,让他们停止你的服务活动。

I am developing a multi player game app for the android. One of the participant is acting as the host (the one who created the game instance) and each of the other participants is connecting to the host using bluetooth.

My question is as follows, this host has some threads running in order to communicate and holds all the open connections. I've read that my Activities can be destroyed temporary and restored later and I should use the onSaveInstanceState mechanism for that. However, I am talking about an app that is acting as a "server" for the game and it has open connections and threads that serves the other clients, what happens to these connections and threads if the OS is deciding to destroy my activity? Are they terminated? If so what would be the recommended pattern in order to implement this properly, How can I keep a connection on the onSaveInstanceState bundle? it is not serializable. The same goes for the threads, do I need to recreate and destroy them upon destroy and when the activity is restored?? if I hold all this state in some static class that represents the game state? If I do so, will then the OS not destroy my threads/connections?

I looked at the sample Bluetooth chat that comes with the SDK and I so there's no handling of the onSaveInstanceState so it is very unclear what should I do.

Thank you!

解决方案

what happens to these connections and threads if the OS is deciding to destroy my activity?

If you created those threads in an activity, you better stop those threads, or you will leak them.

How can I keep a connection on the onSaveInstanceState bundle?

You can't.

The same goes for the threads, do I need to recreate and destroy them upon destroy and when the activity is restored?

If they were started by the activity, then, yes, please.

if I hold all this state in some static class that represents the game state? If I do so, will then the OS not destroy my threads/connections?

Please put your server logic (threads, connections, server-side game logic) in an Android service. Please use startForeground() to keep that service going, putting a Notification in the status bar along the way so the user has an easy way to get back to the activity that allows them to stop your service.

这篇关于保持线程和连接状态的Andr​​oid应用程序中使用的onSaveInstanceState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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