简体   繁体   English

为什么AsyncTask阻止UI线程?

[英]Why is AsyncTask Blocking UI Thread?

Why does my AsyncTask block the UI Thread? 为什么我的AsyncTask会阻止UI线程?

My application becomes unresponsive while the AsyncTask is taking place. AsyncTask发生时,我的应用程序变得无响应。

This is called in the UI Thread: 这在UI线程中称为:

new AsyncFetch().execute();

This is my AsyncTask class: 这是我的AsyncTask类:

class AsyncFetch extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... args) {
        String data = null;
        try {
            data = DataHandler.httpFetch(DataHandler.API_URL);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    protected void onPostExecute(String data) {
        DataHandler.AsyncFetchResult(data);
    }

}

onPostExecute runs on the UI thread. onPostExecute在UI线程上运行。 Avoid doing too much work here. 避免在这里做太多的工作。 Do the heavy stuff in doInBackground and then just update the UI (or whatever you need to do) in onPostExecute . 做重的东西在doInBackground ,然后只更新UI(或任何你需要做的)在onPostExecute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM