Restarting/Pausing Thread in onResume/onPause

我怕爱的太早我们不能终老 提交于 2019-12-07 01:15:44

问题


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.


回答1:


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.




回答2:


This is what I did:

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
           if (thread.getState() == Thread.State.TERMINATED){
              CreateThread(getHolder(),getContext());
           }
           thread.setRunning(true);
           thread.start();
    }
  • In CreateThread you should have the thread = new MyThread(...);
  • the setRunning (boolean mRun) use a boolean to start/stop the run function (I think I was inspired by the LunarLander);

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:

// Variables declarations

public MyGameThread CreateThread(...){
thread = new MyGameThread(holder, context, new Handler() {
// and so on....
});
}

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:

  • SharedPreferences: an xml will be created and saved locally with variables that persist even after the end of the app;
  • a SQL db if you would manage more than 5-10 variables because in this case the use of the former option would be difficult.



回答3:


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 your while loop inside the thread's run() 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.




回答4:


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:

1 - Save the state of the application (all game variables, states, etc)
2 - Kill the surfaceView by calling suspend.
3 - Kill the other thread (we'll call it Thread B).

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:

  • In the while() loop of your thread, have a boolean 'run' which while(run) will execute the code. When you change run to false, the thread exits.
  • If your thread sleeps (I assume it might do since its a game and will be running w.r.t time), catch the 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.



来源:https://stackoverflow.com/questions/7068443/restarting-pausing-thread-in-onresume-onpause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!