AsyncTask在Dialog中不显示ProgressDialog

时间:2011-11-24 11:34:32

标签: android dialog android-asynctask progressdialog

我在自定义对话框中使用AsyncTask。 AsyncTask在活动中工作正常,但内部对话框不显示ProgressDialog。 尝试从对话框,显示对话框,应用程序上下文的活动中执行特权上下文。 结果相同:我的屏幕上没有显示ProgressDialog。

private class ClearCache_Task extends AsyncTask<Integer,Integer,Integer>
{
private ProgressDialog progressBar;
private ArrayList<String> m_subfolders;
private final Context m_con;

public ClearCache_Task(Context con, ArrayList<String> subfolders)
{
        m_con = con;
        m_subfolders = subfolders;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressBar = new ProgressDialog(m_con);
        progressBar.setIndeterminate(true);
        progressBar.setCancelable(false);
        progressBar.setMessage("Please wait...");
        progressBar.show();
    }
    @Override
    protected void onPostExecute(Integer result)
    {
        super.onPostExecute(result);
        progressBar.dismiss();
    }
    @Override
    protected Integer doInBackground(Integer... params)
    {
        try
        {
          m_KFileStorage.ClearCache(m_subfolders);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.e(AppTag, e.toString());
        }
        return 0;
    }

2 个答案:

答案 0 :(得分:0)

如何将对话框附加到活动?

我就是这样做的:

活动:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage(Application.string(R.string.dialog_message) + "...");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}

在AsyncTask中:

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

有关Dialog的更多信息,请参阅此处:http://developer.android.com/guide/topics/ui/dialogs.html

答案 1 :(得分:0)

抱歉问题。 我错误地调用旧代码而不是调用asynctask。:)