如何使用asynctask显示倒计时的进度条?

wya*_*att 7 android countdown android-ui android-asynctask

在我的应用程序中,我希望用户按下按钮然后等待5分钟.我知道这听起来很可怕但只是顺其自然.应在进度条中显示5分钟等待时间内剩余的时间.

我正在使用带有文本视图的CountDownTimer进行倒计时,但我的老板想要看起来更好的东西.因此进度条的推理.

Sus*_*hil 35

你可以这样做..

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DOWNLOAD_PROGRESS:
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("waiting 5 minutes..");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        return mProgressDialog;
    default:
    return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后写一个异步任务来更新进度..

private class DownloadZipFileTask extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @Override
    protected String doInBackground(String... urls) {
        //Copy you logic to calculate progress and call
        publishProgress("" + progress);
    }

    protected void onProgressUpdate(String... progress) {        
    mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String result) {           
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该可以解决你的目的,它甚至不会阻止UI踏板..

  • 不推荐使用:http://stackoverflow.com/questions/10285047/showdialog-deprecated-whats-the- alternative (3认同)