0

我有一个AsyncTask下载文件的doInBackground()方法。我在发布它之前测试我的应用程序。当我手动强制退出应用程序并重新启动它时,doInBackground()似乎调用了 ,但没有调用onProgressUpdate()and onPostExecute()。我的onPostExecute()调用方法会导致 UI 更改。

如何确保onPostExecuteandonProgressUpdate()被调用?我还看到以下警告:

Activity停止超时ActivityRecordActivity销毁超时ActivityRecordActivity空闲超时ActivityRecord

代码:

public void OnCreate(){

    new RefreshMyDashboardTask().execute();
}

private class RefreshMyDashboardTask extends AsyncTask<Void, Void, Long> {

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... params) {
        super.onProgressUpdate(params);
    }

    @Override
    protected Long doInBackground(Void... params) { 
        // my server calls are called here.
    }

    @Override
    protected void onPostExecute(Long result) {
    }
}
4

1 回答 1

1

If you kill your app, or if the system kills it, then AsyncTask threads for your app get killed. They're all part of your app's process; threads can't survive outside the process that started them.

Even an IntentService might not survive a force quit. That's why task killers are such a bad idea.

In short, what you're seeing is expected behavior.

于 2013-02-15T00:23:19.017 回答