1

I have a problem with a thread in surfaceview. I can't understand how to onPause/onResume when I lock my phone. Whatever I do, the thread doesn't respond after locking/unlocking the phone.

In the activity:

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        surfaceView.SurfaceView_OnResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        surfaceView.SurfaceView_OnPause();

    }

In the surfaceview

    public void SurfaceView_OnResume() {
            if (null != surfaceViewThread) {

                surfaceViewThread.setRunning(true);
                surfaceViewThread.notify();
            }

        }

        public void MySurfaceView_OnPause() {           
            surfaceViewThread.setRunning(false);

        }

@Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        myGameThread.setRunning(false);
        while (retry) {
            try {
                myGameThread.join();
                retry = false;
            } catch (InterruptedException e) {
            }
        }
    }

In the thread:

public void setRunning(boolean run) {
        runFlag = run;

    }
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
Anton Sobolev
  • 135
  • 2
  • 3
  • 11

1 Answers1

0

Thread.setRunning(boolean b) is not in the Android API. You can check it here. http://developer.android.com/reference/java/lang/Thread.html

And if I need a Thread, I prefer to use Runnable(Interface), not Thread(Object).

To control my Thread cycle, I will design my method:run() like this...

run(){
    while(threadRun){
            ...//What you want to do in the thread.
            while(threadPause){
            }
    }
}

The Boolean:threadRun will turn to false in Activity.onDestroy(), or any other time you really want to shut down the thread.

The Boolean:threadPause ... turn to false in Activity.onPause() and turn to true in Activity.onResume().

hsu.tw
  • 148
  • 2
  • 10