如何正确使用的AsyncTask [英] How to properly use AsyncTask

查看:129
本文介绍了如何正确使用的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉从相机表面preVIEW帧缓冲和发送这些缓冲区给另一个函数进行处理/在网络上某处公布。

Android开发人员指南给出了这样的例子:
http://developer.android.com/reference/android/os/AsyncTask.html

 私有类DownloadFilesTask扩展的AsyncTask< URL,为Integer,Long> {
     保护龙doInBackground(URL网址...){
         诠释计数= urls.length;
         长总计TOTALSIZE = 0;
         的for(int i = 0; I<计数;我++){
             总计TOTALSIZE + = Downloader.downloadFile(网址[I]);
             publishProgress((INT)((I /(浮点)数)* 100));
         }
         返回总计TOTALSIZE;
     }     保护无效onProgressUpdate(整数...进度){
         setProgressPercent(进展[0]);
     }     保护无效onPostExecute(长效){
         的ShowDialog(已下载+结果+字节);
     }
}

下面是你如何执行它:

 新DownloadFilesTask()执行(URL1,URL2,URL3)。

这是整个操作的一个大的AsyncTask(换句话说,是循环要坐在那里,等待对每个文件同步下载),而整个任务在后台工作,或者是这将启动对多个线程这是通过每个URL?

相关到我的项目,这将是合理的,简单地创建一个全新的AsyncTask每一次我想捕捉帧缓冲区?沿着线的东西:

  //每一帧缓冲区我想处理:
    新FrameProcessorTask()执行(字节[]);


解决方案

有一个线程是在循环中的数据。

您可以做后者了。

通过第二个选项,你需要采取堆的大小考虑进去,因为手机只有内存和许多并行线程数量有限,在大缓冲区所有下载可能溢出的可用内存。

I want to capture a frame buffer from a camera surface preview and send those buffers to another function to be processed / POSTed somewhere on the web.

The Android Dev guide gives this example: http://developer.android.com/reference/android/os/AsyncTask.html

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
}

Here's how you execute it:

new DownloadFilesTask().execute(url1, url2, url3);

Is this entire operation one big AsyncTask (in other words, is that loop going to sit there and wait for each file to download synchronously) while the entire task works in the background, or is this going to start up multiple threads for each URL that was passed?

Related to my project, would it be reasonable to simply create a brand new AsyncTask every single time I want to capture a frame buffer? Something along the lines of:

//for each frame buffer i want to process:
    new FrameProcessorTask().execute(byte[]);

解决方案

It is one thread that is looping over the data.

You can do the latter too.

With the 2nd option, you need to take the heap size into account, as the handset only has a limited amount of memory and many parallel threads, all downloading in big buffers may overrun the available memory.

这篇关于如何正确使用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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