AsyncTask始终在运行,但尚未完成

时间:2018-07-12 06:36:56

标签: android android-asynctask

这是我从Asynctask开始的班级:

public class HttpAsyncTaskSendData extends AsyncTask<String, Void, String> {

    public interface SendDataCallback {
        void onComplete(AsyncTask task,String nidSave, String guid);

        void onError(String msg);
    }
    ...

    @Override
    protected String doInBackground(String... urls) {
        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        return PostJson.POST(urls[0], urls[1]);
    }

    @Override
    protected void onPostExecute(String result) {


            if (result != null) {
    ...
              dataCallback.onComplete(this,res.getString("NidSave"), guid);

                } else {
                    dataCallback.onError("No response from server");
                }
            }
   ....
        }

我在这样的活动中使用此类:

sendDataQueue.add(httpAsyncTaskSendData = new HttpAsyncTaskSendData(cur.getString(cur.getColumnIndex("Guid")),
        cur.getString(cur.getColumnIndex("SendStateGps")), datasource, database1
        , Housing, new HttpAsyncTaskSendData.SendDataCallback() {
    @Override
    public void onComplete(AsyncTask task ,String nidSave, String guid) {
        if (task.getStatus().equals(AsyncTask.Status.FINISHED) &&
                !nidSave.equals("00000000-0000-0000-0000-000000000000")) {
            sendDataQueue.remove(task);
        } else {
            queueHasError = true;
        }

        if (!queueHasError) {
            if (sendDataQueue.size() == 0) {
                SendGps(nidSave, guid);
            }
        }
    }

    @Override
    public void onError(String msg) {
        queueHasError = true;
        toastError(DetailsActivity.this, msg);
    }
}).executeOnExecutor((AsyncTask.THREAD_POOL_EXECUTOR),
        AppUtil.getConfig(DetailsActivity.this,
                MainActivity.SETTING_GEO_SERVICE) + "SaveVisit/",
        json.toString()));

我的问题是,为什么当我检查Asyncktask status时在onComplete回调中处于Running模式?并且不在Finnish中。

我的onComplete回调调用了onPostExecute,在本节中,我的任务应该完成它的工作!!!但仍在运行吗?发生什么了,我有什么问题? enter image description here

1 个答案:

答案 0 :(得分:3)

onPostExecute尚未完成,因此AsyncTask的状态未完成

AsyncTask.Status

  

完成   指示AsyncTask.onPostExecute(Result)已完成。