使用 onSaveInstanceState 在 Android 应用程序中保留线程和连接状态?

发布于 2024-10-01 02:23:38 字数 490 浏览 0 评论 0原文

我正在为 Android 开发一个多人游戏应用程序。 其中一名参与者充当主机(创建游戏实例的人),其他每个参与者都使用蓝牙连接到主机。

我的问题如下,该主机有一些线程正在运行以便进行通信并保留所有打开的连接。我读到我的 Activity 可以暂时销毁并稍后恢复,我应该使用 onSaveInstanceState 机制来实现这一点。但是,我说的是充当游戏“服务器”的应用程序,它具有为其他客户端提供服务的开放连接和线程,如果操作系统决定破坏我的活动,这些连接和线程会发生什么?他们被终止了吗?如果是这样,为了正确实现这一点,推荐的模式是什么,如何在 onSaveInstanceState 包上保持连接?它不可序列化。线程也是如此,我是否需要在销毁时以及活动恢复时重新创建和销毁它们?如果我将所有这些状态保存在代表游戏状态的某个静态类中?如果我这样做,操作系统不会破坏我的线程/连接吗?

我查看了 SDK 附带的示例蓝牙聊天,因此没有处理 onSaveInstanceState,因此非常不清楚我应该做什么。

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-10-08 02:23:38

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

如果您在活动中创建了这些线程,则最好停止这些线程,否则将会泄漏它们。

如何保持 onSaveInstanceState 捆绑包的连接?

你不能。

线程也是如此,我是否需要在销毁时以及活动恢复时重新创建和销毁它们?

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

如果我将所有这些状态保存在代表游戏状态的某个静态类中?如果我这样做,操作系统不会破坏我的线程/连接吗?

请将您的服务器逻辑(线程、连接、服务器端游戏逻辑)放入 Android 服务中。请使用 startForeground() 来保持该服务继续运行,并在状态栏中放置一个通知,以便用户可以轻松返回到允许的 Activity他们停止你的服务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文