从AlertDialog(Android版)启动一个长时间运行的任务 [英] Launching a long running task from an AlertDialog (Android)

查看:193
本文介绍了从AlertDialog(Android版)启动一个长时间运行的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AlertDialog和它有一个按钮,而如果选择,将启动文件上传到这样的远程服务器:

I have an AlertDialog in and it's got a button, which, if selected, will initiate a file upload to a remote server like this:

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

这工作得很好,唯一的问题是,uploadFile()有可能需要较长的时间(30秒到2分钟)。我想用一个进度对话框(即使是一个不确定的),以取代AlertDialog,但我不能看到如何从另一个发布一款对话?

This works fine, the only problem is that uploadFile() can potentially take a long time (30 seconds to 2 minutes). I would like to replace the AlertDialog with a progress dialog (even an indeterminate one), but I can't see how to launch one dialog from another?

任何人都可以提供一些建议了,我怎么能做到这一点。

Can anyone offer some advice for a how I could accomplish this.

谢谢,
Jarabek

Thanks, Jarabek

推荐答案

您可以使用的AsyncTask 在这里,让你的 ProgressDialog preExecute(),并呼吁在 doingInBackground()的方法和解雇ProgressDialog中在 PostExecute()

You can use AsyncTask here, keep your ProgressDialog on the PreExecute() and call the method in the doingInBackground() and dismiss the ProgressDialog in the PostExecute().

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

然后,只需调用 MyAsyncTask 使用新MyAsyncTask()执行();

这篇关于从AlertDialog(Android版)启动一个长时间运行的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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