发生 onPause 时我的线程没有停止,导致 onResume 出现“线程已启动”logcat 错误,因为我不能运行两个线程实例。此时如何终止线程?我相信我需要做类似的事情:
gameLoopThread.setRunning(false);
但是我不能将它添加到我的 balloonBasic onPause 中,我认为上下文是错误的。所以请帮忙,代码如下所示。(代码示例真的很有帮助,谢谢)
我的活动:
public class balloonBasic extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameViewBasic(this));
}
public void onResume() {
super.onResume();
SoundManager.getInstance();
SoundManager.initSounds(this);
SoundManager.loadSounds();
}
public void onPause() {
super.onPause();
//what do I put here?
}
}
我的表面观点:
public class GameViewBasic extends SurfaceView {
...abbreviated declarations...
private static SurfaceHolder holder;
private static GameLoopThreadBasic gameLoopThread;
public GameViewBasic(Context context) {
super(context);
gameLoopThread = new GameLoopThreadBasic(this);
holder = getHolder();
holder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder)
{
gameLoopThread.setRunning(false);
}
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
mapspritegraphics();
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
});
}
...abbreviated for brevity....
我的线程: public class GameLoopThreadBasic extends Thread { private GameViewBasic view; 私有易失性布尔运行=假;
public GameLoopThreadBasic(GameViewBasic gameViewBasic) {
this.view = gameViewBasic;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run() {
long ticksPS = 25; // =(1000/fps) ie 25ticksPS = 40fps
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} catch (Exception f) {} //
finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = (ticksPS - (System.currentTimeMillis() - startTime));
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
}