在ExitDialog上暂停线程

时间:2011-12-22 12:45:36

标签: java android

我应该怎么做才能在pause()处调用线程的showExitDialog()方法? 这是开始游戏课程

package game.mainmenu;


import game.view.ViewPanel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class StartGame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );

        setContentView(new ViewPanel(this));      

    }

    @Override
    protected void onPause() {
        super.onPause();
        //saveScores();
        this.finish();
        System.exit(1);// pause game when Activity pauses
    }


    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {

        if (pKeyCode == KeyEvent.KEYCODE_BACK
                && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
            showExitDialog();

            return true;
        }

        return super.onKeyDown(pKeyCode, pEvent);
    }

    public void showExitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(StartGame.this);
        builder.setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setTitle("EXIT")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                StartGame.this.finish();
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = builder.create();
        //alert.setIcon(android.R.drawable.star_on);
        alert.show();

    }
}

这是主线程的课程

public class ViewManager extends Thread 
{
    //some states here
    public static final int STATE_LOSE = 1;
    public static final int STATE_PAUSE = 2;
    public static final int STATE_READY = 3;
    public static final int STATE_RUNNING = 4;
    public static final int STATE_WIN = 5;

    //..some not mention code here../

    public ViewManager(SurfaceHolder surfaceHolder, Context context)
    {
        mSurfaceHolder = surfaceHolder;
        mRunning = false;
        mHealthBar = new Rect(0,0,0,0);
        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setARGB(255, 0, 255, 0);
        mLinePaint.setTextSize(16);
        mLinePaint.setStrokeWidth(3);
        mContext = context;
        Resources res = context.getResources();

        //..some not mention code here../
        InitElements(res);

        mHero = new PlayerAnimated(mPlayerImage, FIELD_WIDTH/2, 600, 64, 64, 3, 3, context, mEnemiesList);
        //mBoom = new Explosion(mExplosionImage, 200, 500, 64, 64, 7, 7);
        mEnemyImage = BitmapFactory.decodeResource(res, R.drawable.enemyone);

        setState(STATE_RUNNING);

    }


    /**
        * threads state
        * @param running
        */
       public void setRunning(boolean running)
       {
           mRunning = running;
       }

       //..some not mention code here../

       public void run()
       {
           while (mRunning)
           {
               Canvas canvas = null;
               try
               {
                   // подготовка Canvas-а
                   canvas = mSurfaceHolder.lockCanvas();
                   synchronized (mSurfaceHolder)
                   {
                       if(mMode == STATE_RUNNING){

                           // draw if not paused
                           addEneimes(mContext);

                           updateStuff();

                           doDraw(canvas);
                       }
                       else
                       {
                           pauseDraw(canvas);
                       }
                       ViewPanel.displayFps(canvas, aString);

                       aString = Integer.toString(hudscore.getScore());

                   }
               }
               catch (Exception e) { }
               finally
               {
                   if (canvas != null)
                   {
                       mSurfaceHolder.unlockCanvasAndPost(canvas);
                   }
               }
           }
       }

       //..some not mention code here../

    public void pause() {
        synchronized (mSurfaceHolder) {
           setState(STATE_PAUSE);
           mLastFiredTime = System.currentTimeMillis() + 100;
        }
    }

    public void unpause() {
        //
        synchronized (mSurfaceHolder) {
            setState(STATE_RUNNING);
            mLastFiredTime = System.currentTimeMillis() + 100;
        }
    }

    public void setState(int mode) 
    {
        mMode = mode;
    }

    public void pauseDraw(Canvas canvas)
    {
         canvas.drawBitmap(Bitmap.createBitmap(FIELD_WIDTH, FIELD_HEIGHT, Bitmap.Config.RGB_565), 0, 0, null);
    }
}

1 个答案:

答案 0 :(得分:0)

不是一个明确的问题,因为你没有告诉我们你想在哪里创建并在主代码中启动Thread。我们假设它在onCreate里面:

public class StartGame extends Activity {

    private ViewManager viewManager = new ViewManager();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );

        setContentView(new ViewPanel(this));    

        viewManager.start(); // start viewManager thread
    }

    // other methods

    public void showExitDialog() {

        viewManager.pause(); // call pause

        // rest of code
    }
}