后退按钮不工作,而progressDialog运行 [英] BACK Button is not working ,while progressDialog is running

查看:167
本文介绍了后退按钮不工作,而progressDialog运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题;我希望你能帮助我。 虽然progressDialog运行时,用户presses BACK按钮。当前活动progressDialog去背景,但progressDialog运行。

I have a little problem; I hope you can help me. While progressDialog is running, the user presses the BACK Button. The current Activity progressDialog goes to Background, but progressDialog is running.

我的问题是,当用户点击返回按钮,progressDialog应该是前台的活动,停止目前的进展,并问:你想继续还是退出?

My problem is that when the user clicks on the BACK button, progressDialog should be foreground Activity, stop current progress and ask "Do you want to continue or exit?"

如果用户presses继续,progressDialog应继续余下的工作。

If the user presses Continue, progressDialog should continue the remaining work.

否则,关闭当前的活动。

Otherwise, close the current activity.

code的位置:

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;
    }
}

}

推荐答案

类似的问题,当progressDialog.setCancelable(真)的设置,那么打后退按钮不执行code写的后退按钮code,但只是取消的进展再次dialog..Hitting的关键工作。

Similar issue , When progressDialog.setCancelable(true) is set then hitting back button does not execute code written in back button code but just cancels the progress dialog..Hitting the key again works.

我要取消的进度对话框,并执行一些片code一起这是行不通的。 单击后退按钮两次没有道理给我。

i want to cancel the progress dialog and execute some piece of code together which is not working. Clicking back button twice does not make sense to me.

这篇关于后退按钮不工作,而progressDialog运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆