当progressDialog正在运行时,BACK按钮不起作用

时间:2011-02-07 09:25:59

标签: android android-emulator android-layout

我有一点问题;我希望你能帮助我。 在progressDialog运行时,用户按下BACK按钮。当前的Activity progressDialog转到Background,但progressDialog正在运行。

我的问题是当用户点击BACK按钮时,progressDialog应该是前台Activity,停止当前进度并询问“你想继续还是退出?”

如果用户按下继续,progressDialog应继续剩下的工作。

否则,请关闭当前活动。

此处的代码:

public class SampleExample extends Activity {
    static final int PROGRESS_DIALOG = 0;
    Button button;
    TextView download;
    ProgressThread progressThread;
    ProgressDialog progressDialog;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    download = (TextView) findViewById(R.id.download);
    button = (Button) findViewById(R.id.progressDialog);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            showDialog(PROGRESS_DIALOG);

        }
    });

}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Handle the back button
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Ask the user if they want to quit


        new AlertDialog.Builder(this).setIcon(
                android.R.drawable.ic_dialog_alert).setTitle("Exit")
                .setMessage("Are you sure you want to leave?")
                .setNegativeButton(android.R.string.cancel, null)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Exit the activity
                                SampleExample.this.finish();
                            }
                        }).show();

        // Say that we've consumed the event

        return true;
    }

    return super.onKeyDown(keyCode, event);
}




protected Dialog onCreateDialog(int id) {

    switch (id) {
    case PROGRESS_DIALOG:
        progressDialog = new ProgressDialog(SampleExample.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...");
        download.setText("Now downloading.......");
        progressThread = new ProgressThread(handler);
        progressThread.start();
        progressDialog.setCancelable(true);


        return progressDialog;
    default:
        return null;
    }
}

// Define the Handler that receives messages from the thread and update the
// progress
final Handler handler = new Handler() {
    public void handleMessage(Message msg) {

        int total = msg.getData().getInt("total");
        progressDialog.setProgress(total);

        if (total >= 100) {

            download.setText(" download is completed.");
            dismissDialog(PROGRESS_DIALOG);
            progressThread.setState(ProgressThread.STATE_DONE);
        }
    }
};

/** Nested class that performs progress calculations (counting) */
private class ProgressThread extends Thread {
    Handler mHandler;
    final static int STATE_DONE = 0;
    final static int STATE_RUNNING = 1;
    int mState;
    int total;

    ProgressThread(Handler h) {
        mHandler = h;
    }


    public void run() {
        mState = STATE_RUNNING;
        total = 0;

        while (mState == STATE_RUNNING) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                Log.e("ERROR", "Thread Interrupted");

            }
            Message msg = mHandler.obtainMessage();
            Bundle b = new Bundle();
            b.putInt("total", total);
            msg.setData(b);
            mHandler.sendMessage(msg);

            total++;
        }
        Log.d("SampleExample", "6666 run () 6666 End");
    }

    /*
     * sets the current state for the thread, used to stop the thread
     */
    public void setState(int state) {
        mState = state;
    }
}

}

4 个答案:

答案 0 :(得分:3)

您需要覆盖后退按钮事件。你可以这样做:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && progressDialog.isShowing()) {
        Log.d(this.getClass().getName(), "back button pressed");//write your own logic here, Whatever you want to do
    }
    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:3)

好吧,我有同样的问题。对我有用的最简单的方法是使用progressDialog.setCancelable(true)..这声明对话框是否可以通过按后退键来取消。试试看,让我知道它是否适合你。祝你好运

答案 2 :(得分:3)

类似的问题,当设置progressDialog.setCancelable(true)时,按下后退按钮不会执行后退按钮代码中写入的代码,只是取消进度对话框。再次按下该键可以正常工作。

我想取消进度对话框并执行一些无法正常工作的代码。 单击两次后退按钮对我没有意义。

答案 3 :(得分:2)

您可以使用progressDialog.setCancelable(true),然后使用progressDialog.setOnCancelListener()提供OnCancelListener来执行您要执行的代码onCancel()

请参阅Dialogs上的Android文档。