2

我在关闭线程时遇到问题。我将使用 onStop、onPause 和 onDestroy 关闭线程。这是我在活动类中的来源:

   @Override
protected void onStop(){
   super.onStop();
   finish();
} 

@Override
protected void onPause()  { 
     super.onPause(); 
     finish();
    } 

@Override
public void onDestroy() {
        this.mWakeLock.release();
        super.onDestroy();
}

和线程类:

public class GameThread extends Thread {

private SurfaceHolder mSurfaceHolder;
private Handler mHandler;
private Context mContext;
private Paint mLinePaint;
private Paint blackPaint;

//for consistent rendering
private long sleepTime;
//amount of time to sleep for (in milliseconds)
private long delay=1000/30;
//state of game (Running or Paused).
int state = 1;
public final static int RUNNING = 1;
public final static int PAUSED = 2;
public final static int STOPED = 3;

GameSurface gEngine;

public GameThread(SurfaceHolder surfaceHolder, Context context, Handler handler,GameSurface gEngineS){

    //data about the screen
    mSurfaceHolder = surfaceHolder;
    mHandler = handler;
    mContext = context;
    gEngine=gEngineS;
}

//This is the most important part of the code. It is invoked when the call to start() is
//made from the SurfaceView class. It loops continuously until the game is finished or
//the application is suspended.
private long beforeTime;
@Override
public void run() {

    //UPDATE
    while (state==RUNNING) {
        Log.d("State","Thread is runnig");
        //time before update
        beforeTime = System.nanoTime();
        //This is where we update the game engine
        gEngine.Update();

        //DRAW
        Canvas c = null;
        try {
            //lock canvas so nothing else can use it
            c = mSurfaceHolder.lockCanvas(null);
            synchronized (mSurfaceHolder) {
                //clear the screen with the black painter.
                //reset the canvas
                c.drawColor(Color.BLACK);
                //This is where we draw the game engine.
                gEngine.doDraw(c);
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                mSurfaceHolder.unlockCanvasAndPost(c);
            }
        }



        this.sleepTime = delay-((System.nanoTime()-beforeTime)/1000000L);

        try {
            //actual sleep code
            if(sleepTime>0){
                this.sleep(sleepTime);

            }
        } catch (InterruptedException ex) {
            Logger.getLogger(GameThread.class.getName()).log(Level.SEVERE, null, ex);
        }

        while (state==PAUSED){
            Log.d("State","Thread is pausing");
            try {
                this.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}}

我如何从活动类中关闭线程?

4

1 回答 1

0

您可以将 currentState 变量定义为静态变量,然后您可以通过静态函数访问它以将其状态更改为可以停止或暂停线程的状态,但我认为您可以使用最适合此类工作的 Android 内部组件例如服务,您可以向他们发送消息,或者可以将消息发送回您的活动,或者您可以 AsyncTask 。看看这个

于 2012-10-20T10:56:30.327 回答