在 onResume/onPause 中重新启动/暂停线程
我有一个使用 SurfaceView 实现来显示对象的游戏。 我有一个线程,它会不时地将 SurfaceView 绘制到屏幕上。 游戏正在完全运行。 不幸的是,它需要在游戏中断时具有暂停功能。 好吧,我知道我需要操作 onResume
和 onPause
。
但我做不到。该错误将我带回到 SurfaceCreated ,我在其中启动线程,告诉我线程已经启动。我尝试分别在 onResume
和 onPause
上使用 resume
和 suspend
,但没有任何变化。
我怎样才能实现这个目标? 我已经完成了如何使用文件 I/O 处理来保存对象位置。
提前致谢。
I'm have a game that's uses SurfaceView implementation to display the objects.
I have a thread which draws the SurfaceView time-to-time to the screen.
The game is running completely.
Unfortunately, it needed to have a pause function whenever the game is interrupted.
Well, I know that I need to manipulate onResume
and onPause
.
But I can't get it right. The error points me back to surfaceCreated
where I start the thread telling me that the thread has started already. I tried using the resume
and suspend
on the onResume
and onPause
respectively but nothing changed.
How can I achieve this?
I have already done how the objects location would be save using File-I/O handling.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是我所做的:
如果您想正确使用 onPause/onResume,请勿将线程使用的变量放入线程内(如 LunarLander 中所做的那样)。我建议你这样做:
当你通过onPause/onResume时,你的线程将被销毁并重新启动,但如果你将变量放在它外面,你可以在之后继续使用它们。
如果您有重要的内容需要保留,请使用以下选项之一:
This is what I did:
If you want to use properly the onPause/onResume don't put the variables used by your thread inside the thread (as done in LunarLander). I suggest you to do like that:
When you pass through the onPause/onResume, your thread will be destroyed and reneweled but if you put your variables outside it, you can continue to use them after.
If you have something important to preserve, use one of this options:
实际上不建议您自己停止线程,
stop()
方法已被弃用。最简单的解决方案是在线程的 run() 方法内的 while 循环中使用标志。当您需要“停止”线程时,只需将标志设置为 false,线程将不再执行任何操作,尽管它会继续运行。 Android 会在需要时停止你的线程。希望这有帮助。Actually it's not recommended to stop a thread by yourself, the
stop()
method is deprecated. The simplest solution is to use a flag in yourwhile
loop inside the thread'srun()
method. When you need to "stop" the thread, you just drop the flag to false and the thread won't do anything anymore, despite it will keep running. Android will stop your thread when it's needed. Hope this helps.不知道代码的来龙去脉。
要“暂停”线程,您可以实现如下功能:
这取决于
Do Something
是否使您的表面视图无效或以其他方式控制应用程序中的进度。paused
的访问器应该允许您暂停和恢复线程,而不会陷入架构的任何其他部分。Without knowing the ins and outs of your code.
To "Pause" a thread you can implement functionality like so:
This is depending if
Do something
is invalidating your surface view or otherwise controlling progression in your app. An accessor topaused
should allow you to pause and resume your thread without getting caught up in any other bit of architecture.我不确定你在这个问题中是否有一个或两个线程,我假设是 2。当你调用 onPause 时,你需要做三件事:
我认为杀死线程 B 是你的问题。您想要中断线程并告诉它退出,否则当您调用 onPause 时,您的线程仍将执行其操作。然后,当您返回游戏时,线程将尝试再次创建,这会导致问题。有两种方法可以正确终止线程:
while()
循环中,有一个布尔值“run”,while(run)
将执行代码。当您将 run 更改为 false 时,线程将退出。第一个是迄今为止最简单的。
I'm unsure if you've got one or two threads in this question, I'm assuming 2. You need to do three things when you call onPause:
Killing of Thread B is your problem I think. You want to interrupt the thread and tell it to quit, or else when you call onPause your thread will still be doing its thing. Then, when you go back into the game, the thread will try to be created again which causes the problem. There are 2 ways to kill a thread properly:
while()
loop of your thread, have a boolean 'run' whichwhile(run)
will execute the code. When you change run to false, the thread exits.InterruptedException
and then quit there. When you want to kill the thread, you throw the exception to the thread.The first one is by far the easiest.