警报对话框未被取消

时间:2011-12-22 05:44:20

标签: android multithreading android-alertdialog

我正在开发一款游戏。如果用户在播放游戏线程时按下设备后退按钮,屏幕上会显示警告对话框。如果用户不想退出游戏,游戏线程应该恢复警报对话框应该消失..但问题是我可以恢复游戏线程但无法取消警报对话框。下面是我的后退按钮事件代码片段。

    public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {

        MainGamePanel.thread.setRunning(false);

         AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Exit Alert");
          //  alertDialog.setIcon(R.drawable.appicon);

            alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                 finish();
                 return;
            } }); 
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {


                    if(mainscreenOn)
                    {
                         dialog.cancel();
                    }
                    else
                    { 
                        dialog.cancel();
                        GamePanel.thread.setRunning(true);
                         GamePanel.thread.run();
                    }


                return;
            }}); 
             alertDialog.show();

         return true;
     }
     return super.onKeyDown(keyCode, event);
 }

如果mainscreenOn标志为true,那么警告对话框就会被取消..但问题就出现了,如果它是假的。请有人帮帮我...我被困在这...

我开始知道这不是警告对话的问题...这是我的游戏线程的问题。以下是我的游戏线程代码..

    public class GameThread extends Thread {

private static final String TAG = GameThread.class.getSimpleName();

// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private MainGamePanel gamePanel;


// flag to hold game state 
public boolean running;
public void setRunning(boolean running) {
    this.running = running;
}

public GameThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}



@Override
public void run() {
    Canvas canvas;
    Log.d(TAG, "Starting game loop");

    while (running) {

        Log.v(""," ->Inside Thread!" + running);
        canvas = null;
        // try locking the canvas for exclusive pixel editing
        // in the surface
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                // update game state 
                this.gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                this.gamePanel.render(canvas);  

                }
        } finally {
            // in case of an exception the surface is not left in 
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}



 }

当我调试代码时,控件将转到此代码并停留在此处..

3 个答案:

答案 0 :(得分:0)

你需要调用dialog的dismiss()而不是cancel()。

dialog.dismiss();

尝试这个

答案 1 :(得分:0)

对于你取消对话的每个条件。所以我认为你不应该检查 取消。你可以像下面这样做。

alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                        if(!mainscreenOn)
                        {
                             GamePanel.thread.setRunning(true);
                             GamePanel.thread.run();
                        }


                    return;
                }}); 

编辑:

然后尝试使用Positive Button和Negative Button。如果它不起作用,请检查“Yugandhar Babu”提到的点。

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

答案 2 :(得分:0)

我在没有 GamePanel 语句的情况下测试了您的代码,它的工作正常 mainscreenOn等于true和flase

我认为在您的情况下,当 mainScreenOn为false 时,它不会从onClick()返回,这意味着它会在GamePanel.thread.setRunning()GanePanel.thread.run()中出现。


检查此链接可能会对您有所帮助。关于线程的暂停和恢复。

How to pause/resume thread in Android


您在run()之后致电setRunning(true)。我想如果你的主题有Runnable个对象,那么只需要调用run()。如下

Thread t = new Thread(new Runnable() {
      public void run() {
          // your task
      }
});

如果您没有使用Runnable个对象,我认为无需致电run()。无论如何你用running变量控制线程。

请检查一下,我对此也没有信心。