延迟后运行AsyncTask不会触发onPostExecute

时间:2019-01-30 09:42:37

标签: java android android-asynctask

            new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                asyncTask(param);
            }
        }, 5000);

并尝试:

final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "ENTERED");
                asyncTask(param);
            }
        }, 5000);

这部分代码在onCreate方法上运行,由于某种原因,当我调试Log.d并将其放入onPostExecute时,它从未输入。

我需要在5s延迟后运行任务,并从onPostExecute获取结果,是否有解决方法?

谢谢。 onPostExecute:

@Override
protected void onPostExecute(Integer result) {
    Log.d(TAG, "NOPE");
    listener.onFetchFinished(result);
}

也:doInBackground成功返回,因此不是原因。

编辑:粘贴更多代码:

    private void asyncTask(int param) {
    AsyncTask pc = new AsyncTask (getApplicationContext(), listener);
    pc.execute(moreParams);}

1 个答案:

答案 0 :(得分:0)

问题似乎是“从不同线程进行调用”。

这可能对您有用

val handler = Handler(Looper.getMainLooper())
handler.posteDelayed({asyncTask(param)},5000)