在onResume /重新启动的onPause /暂停主题 [英] Restarting/Pausing Thread in onResume/onPause

查看:516
本文介绍了在onResume /重新启动的onPause /暂停主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个游戏,就是使用SurfaceView实现显示的对象。 我有一个线程时间 - 时间哪个绘制SurfaceView到屏幕上。 本场比赛是完全运行。 不幸的是,它需要有每当游戏被中断暂停功能。 好吧,我知道我需要操纵 onResume 的onPause

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.

但我无法得到它的权利。错误指向我回 surfaceCreated 在那里我开始线程告诉我该线程已经开始了。我试着用恢复暂停 onResume 的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.

我怎样才能做到这一点? 我已经做了怎样的目标位置是保存使用文件I / O处理。

How can I achieve this? I have already done how the objects location would be save using File-I/O handling.

在此先感谢。

推荐答案

不知道你的code的来龙去脉。

Without knowing the ins and outs of your code.

要暂停就可以实现像这样功能的线程:

To "Pause" a thread you can implement functionality like so:

while(! this.isInterrupted())
    if(!paused)
    {
        ... Do something ...
    } else { try { Thread.sleep(100) } catch (InteruptedException ie) {} }

这取决于是否做一些事情是无效的表面观或以其他方式控制进程在你的应用程序。一个访问到暂停应该让你暂停和恢复你的线程不被抓到在建筑中的任何其他位。

This is depending if Do something is invalidating your surface view or otherwise controlling progression in your app. An accessor to paused should allow you to pause and resume your thread without getting caught up in any other bit of architecture.

这篇关于在onResume /重新启动的onPause /暂停主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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