Android:如何在主线程中等待AsyncTask完成?

14

我知道你第一个想问的是……为什么在世界上你要使用AsyncTask。

这就是我的问题,我正在开发一些 Android 应用程序(API 7,适用于 Android 2.1 或更高版本),并在模拟器上测试,一切都很顺利,所以我在 HTC Sensation 上进行测试时它说出了 NetworkOnMainThreadExeption!

我正在下载一些图片然后在地图上绘制。

所以为了解决这个问题,我必须把所有的(网络连接)在这种情况下下载图片的工作都放在AsyncTask中进行。

所以我需要一个方法来知道所有图片何时完成,这样我才能开始绘制。

我已经尝试过很多次,但没有结果,我不知道怎么办。我有一种使用 handler 的解决方案,但如果在较慢的网络上运行,我将获得 nullpointer(因为图片还没有被下载)。

所以请帮帮我。

编辑:

这是我的想法:

Bitmap bubbleIcon ;
    onCreate(){
     ...
// i am making call for Async
new ImgDown().execute(url);
//and then i calling functions and classes to draw with that picture bubbleIcon !
DrawOnMap(bubbleIcon);
}




//THIS IS ASYNC AND FOR EX. SUPPOSE I NEED TO DOWNLOAD THE PIC FIRST
     class ImgDown extends AsyncTask<String, Void, Bitmap> {

        private String url;

        public ImgDown() {
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            url = params[0];
            try {
                return getBitmapFromURL(url);
            } catch (Exception err) {
            }

            return null;

        }

        @Override
        protected void onPostExecute(Bitmap result) {
            bubbleIcon = result;
            bubbleIcon = Bitmap
                    .createScaledBitmap(bubbleIcon, 70, 70, true);

        }

        public Bitmap getBitmapFromURL(String src) {
            try {
                Log.e("src", src);
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                // /tuka decode na slika vo pomalecuk kvalitet!
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                Bitmap myBitmap = BitmapFactory
                        .decodeStream(new FlushedInputStream(input));
                Log.e("Bitmap", "returned");
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("getBitmapFromURL", e.getMessage());
                return null;
            }
        }

        class FlushedInputStream extends FilterInputStream {
            public FlushedInputStream(InputStream inputStream) {
                super(inputStream);
            }

            public long skip(long n) throws IOException {
                long totalBytesSkipped = 0L;
                while (totalBytesSkipped < n) {
                    long bytesSkipped = in.skip(n - totalBytesSkipped);
                    if (bytesSkipped == 0L) {
                        int byteValue = read();
                        if (byteValue < 0) {
                            break; // we reached EOF
                        } else {
                            bytesSkipped = 1; // we read one byte
                        }
                    }
                    totalBytesSkipped += bytesSkipped;
                }
                return totalBytesSkipped;
            }
        }
    }

我希望现在更清楚了。


请粘贴相关的源代码。如果您只提供非正式描述,则无法提供帮助。 - Thomas Calc
如果我使用进度对话框,那怎么办,请再仔细阅读我的问题... :/ - user1730007
在等待的同时,是否仍有用户交互?如果没有,则使用进度对话框来显示提示用户等待的对话框。 - WillingLearner
那么你一直告诉我这行代码 "DrawOnMap(bubbleIcon);" 会在进度对话框结束后执行? - user1730007
把它放在onPostExecute里面,是的,就可以了。 - WillingLearner
我不能在那里放置,因为我从其他地方有几个不同的调用到这个函数,所以我需要一种方法来知道AsyncTask何时结束。 - user1730007
4个回答

32
class OpenWorkTask extends AsyncTask {

    @Override
    protected Boolean doInBackground(String... params) {
        // do something
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // The results of the above method
        // Processing the results here
        myHandler.sendEmptyMessage(0);
    }

}

Handler myHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            // calling to this function from other pleaces
            // The notice call method of doing things
            break;
        default:
            break;
        }
    }
};

1
这是来自Java.util吗?它没有一个可被重写的handleMessage..有close(),flush()和publish(LogRecord record)。 - CularBytes
我想发送广播并在适当的位置接收它可能是更合适的解决方案。 - Reaz Murshed

9

您可以使用面向对象编程原则编写自己的Delegate来代表有关任务完成信息的信息:

task_delegate.java

public interface TaskDelegate {
    void TaskCompletionResult(String result);
}

main_activity.java

public class MainActivity extends Activity implements TaskDelegate {

    //call this method when you need     
    private void startAsynctask() {
      myAsyncTask = new MyAsyncTask(this);
      myAsyncTask.execute();
     }

//your code

    @Override
    public void TaskCompletionResult(String result) {
        GetSomethingByResult(result);
    }
}

my_asynctask.java

public class MyAsyncTask extends AsyncTask<Void, Integer, String> {

    private TaskDelegate delegate;

    protected MyAsyncTask(TaskDelegate delegate) {
        this.delegate = delegate;
    }

    //your code 

    @Override
    protected void onPostExecute(String result) {

        delegate.TaskCompletionResult(result);
    }
}

2
class openWorkTask extends AsyncTask<String, String, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        //do something
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // The results of the above method
        // Processing the results here
    }
}

我无法在那里处理结果,因为我从其他地方对该函数进行了几次不同的调用,所以我需要一种方法来知道异步任务何时结束。 - user1730007
当网络数据获取完成时,您可以发送HandlerMessage。 - RookieWen
我该怎么做?你能给我一个例子吗? - user1730007

0
如果我是你,我会使用进度对话框。这样用户就可以看到 ASyncTask 下载图片时发生了什么。在 PostExecute 中,调用主代码中的一个方法来检查图片是否为空。请记住,在 doInBackground 方法中无法更新 UI,因此请在 onPreExecute 或 onPostExecute 中进行任何 UI 工作。
private class DownloadPictures extends AsyncTask<String, Void, String> 
{

    ProgressDialog progressDialog;

    @Override
    protected String doInBackground(String... params) 
    {

        //Download your pictures

        return null;

    }

    @Override
    protected void onPostExecute(String result) 
    {

        progressDialog.cancel();

        //Call your method that checks if the pictures were downloaded

    }

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(
                YourActivity.this);
        progressDialog.setMessage("Downloading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // Do nothing
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接