从AlertDialog(Android)启动长时间运行的任务

jar*_*bek 0 android task android-alertdialog

我有一个AlertDialog,它有一个按钮,如果选中,将启动文件上传到远程服务器,如下所示:

builder.setMessage(text)                
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
uploadFile();
}})
Run Code Online (Sandbox Code Playgroud)

这很好用,唯一的问题是uploadFile()可能需要很长时间(30秒到2分钟).我想用一个进度对话框(甚至是一个不确定的对话框)替换AlertDialog,但是我看不到如何从另一个对话框启动一个对话框?

任何人都可以提供一些建议,我可以做到这一点.

谢谢,Jarabek

Lal*_*ani 5

你可以AsyncTask在这里使用,保持你的ProgressDialog,PreExecute()并在中调用方法doingInBackground()并关闭中的ProgressDialogPostExecute().

private class MyAsyncTask extends AsyncTask<Void, Void, Void>
    {

        ProgressDialog mProgressDialog;
        @Override
        protected void onPostExecute(Void result) {
            mProgressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(ActivityName.this, "Loading...", "Data is Loading...");
        }

        @Override
        protected Void doInBackground(Void... params) {
            uploadFile();
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后只需调用MyAsyncTask使用new MyAsyncTask().execute();