Android:如何在 MainThread 中等待 AsyncTask 完成? [英] Android: how to wait AsyncTask to finish in MainThread?

查看:17
本文介绍了Android:如何在 MainThread 中等待 AsyncTask 完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您首先要做的是...为什么要使用 AsyncTask.

I know that the first you gonna this is... why the heck on the world you then use AsyncTask.

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

So here is my problem i am working on some Android app (API 7 for android 2.1 or higher) , and i am testing on emulator and everything was fine, so then i tested on HTC Sensation and it says NetworkOnMainThreadExeption!

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

I was downloading some pictures and then draw on the map.

所以为了解决这个问题,每次(互联网连接)在这种情况下下载图片我必须放在 AsyncTask 上才能工作.

So to solve this problem every (internet connection) in this case downloading the pictures i must put on AsyncTask to work.

所以我需要一种方法来知道所有图片何时完成,以便我可以开始绘图..

So i need a method how to know when all pictures are done so i can start drawing..

我尝试了很多但没有结果我不知道.我得到了一个带有处理程序的解决方案,但如果在较慢的网络上运行,我会得到空指针(因为没有下载图片).

I was trying so much and no result i have no idea. I got one solution with handler but if run on slower net i get nullpointer(because the pictures are not downloaded).

所以请帮助我.

这是一个想法:

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;
            }
        }
    }

我希望现在更清楚了.

推荐答案

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;
        }
    }
};

这篇关于Android:如何在 MainThread 中等待 AsyncTask 完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆