Android - progressdialog not displaying in AsyncTask

折月煮酒 提交于 2019-12-01 06:27:25

Remove .get() from return new HttpConnection(activity).execute().get(); You are basically locking your UI thread. Once removed it should work as AsyncTasks are expected to work.

The purpose is to be Asynchronous so boolean downloadData() should have a return type of void. If you need to do something with the data then you should implement an interface "listener" and pass it to the AsyncTask.

Example Listener:

class TaskConnect extends AsyncTask<Void, Void, ConnectionResponse> {

    private final AsyncTaskListener mListener;

    /**
     *
     */
    public TaskConnect(AsyncTaskListener listener) {
        ...
        mListener = listener;
    }

    @Override
    protected void onPreExecute() {
        if (mListener != null) {
            mListener.onPreExecute(mId);
        }
    }

    @Override
    protected ConnectionResponse doInBackground(Void... cData) {
        ...
        return responseData;
    }

    @Override
    protected void onPostExecute(ConnectionResponse response) {
        if (mListener != null) {
            mListener.onComplete(response);
        } else {
            LOG.w("No AsyncTaskListener!", new Throwable());
        }
    }
}

public interface AsyncTaskListener {
    public abstract void onPreExecute(int id);
    public abstract void onComplete(ConnectionResponse response);
}

My issue was not the common issue of others where they were calling get() method after execute() method. My issue was the Context I was passing to my AsyncTask method. I have a settingsActivity and I have a ReadMeActivity that calls the asynctask task. Instead of using the context in which is was being called (ReadMeActivity.this) I used the settingsActivity which prevented it from being seen. Once I switched it and passed it the context in which the activity was being called it worked.

Hope it helps someone else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!