简体   繁体   English

进度对话框未显示在AsyncTask中

[英]Progress Dialog not showing up in AsyncTask

I have an Android application with an AsyncTask which is responsible for downloading a file from the internet. 我有一个带有AsyncTask的Android应用程序,该应用程序负责从互联网下载文件。 This AsyncTask is executed when clicking on an item in a Listview. 单击Listview中的项目时,将执行此AsyncTask。 So I have a custom adapter and in the OnItemClickListener of the Listview, I start the download and execute the AsyncTask. 因此,我有一个自定义适配器,并且在Listview的OnItemClickListener中,我开始下载并执行AsyncTask。

Now, my adapter contains the following code to start the AsyncTask named FileDownloader: 现在,我的适配器包含以下代码以启动名为FileDownloader的AsyncTask:

@Override
public void onClick(View view) {
     try {
         FileDownloader fd = new FileDownloader(activity);
         // some irrelevant code here
         String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();
     }
     catch(Exception e) { e.printStackTrace(); }
}

Activity is a private field that is passed to the adapter in the constructor of the adapter: 活动是一个私有字段,该字段在适配器的构造函数中传递给适配器:

public GameHistoryAdapter(Activity a, int selectedIndex) {
    this.activity = a;
}

The FileDownloader class contains an OnPreExecute method where I want to show the progress dialog on the activity: FileDownloader类包含一个OnPreExecute方法,我想在其中显示活动的进度对话框:

@Override
protected void onPreExecute() {
    super.onPreExecute();

    dialog = new ProgressDialog(activity);
    dialog.setMessage("Downloading...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    dialog.show();
}

But whatever I try, the dialog does not appear. 但是,无论我如何尝试,都不会出现该对话框。 When I create an alert dialog in the OnPostExecute method of the AsyncTask, the dialog will show. 当我在AsyncTask的OnPostExecute方法中创建警报对话框时,将显示该对话框。

@Override
protected void onPostExecute(String res)
{
    super.onPostExecute(res);
    dialog.hide();

    new AlertDialog.Builder(activity)
            .setTitle(activity.getString(R.string.save_pdf_title_text))
            .setMessage(activity.getString(R.string.save_pdf_text) + PDFFileName)
            .setPositiveButton(activity.getString(R.string.close_text), null)
            .setIcon(android.R.drawable.ic_dialog_info)
            .show();
}

Does anyone know why the dialog is not appearing on my activity? 有谁知道为什么对话框未出现在我的活动中?

Does anyone know why the dialog is not appearing on my activity? 有谁知道为什么对话框未出现在我的活动中?

Yes, the following line of code... 是的,下面的代码行...

String filepath = fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString()).get();

Don't EVER use the get() method of AsyncTask . 永远不要使用AsyncTaskget()方法。 It will block the main / UI thread and makes the whole point of an AsyncTask redundant. 它将阻塞主/ UI线程,并使AsyncTask的整个点变得多余。 In other words get() turns it into a synchronous process instead of an asynchronous one. 换句话说, get()将其变成一个同步进程,而不是一个异步进程。

The fact you can show a dialog in onPostExecute(...) is simply because it will be called after the blocking call to get() has returned. 您可以在onPostExecute(...)显示对话框的事实仅是因为将在对get()的阻塞调用返回后调用该对话框。 This means the main / UI thread will no longer be frozen (blocked) and UI updates can be made once again. 这意味着主/ UI线程将不再被冻结(阻止),并且可以再次进行UI更新。

Remove get() from your call to execute(...) and instead just use... 从对execute(...)调用中删除get() ,而仅使用...

fd.execute("http://myurl.com/img.png", PDFFileName, GameHistoryAdapter.this.gameInfo.toString());

...then in your onPostExecute(...) method set you filepath variable to what it should be. ...然后在onPostExecute(...)方法中将文件filepath变量设置为应该的值。

I don't know who added the get() method to AsyncTask but if I ever find them I'll have some serious words to say. 我不知道谁在AsyncTask添加了get()方法,但是如果我找到了它们,我会说些严肃的话。 It has little or no use and causes a lot of people a lot of confusion. 它几乎没有用处,甚至没有用处,并且使很多人感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM