简体   繁体   English

已经开始在AsyncTask之间交换对象(或消息)

[英]Exchange of objects (or message) between AsyncTasks already started

I have three AsyncTask: The first captures the image frame (more than one per second) Mat type and should pass it to the second AsyncTask. 我有三个AsyncTask:第一个捕获图像帧(每秒超过一个)Mat类型,并将其传递给第二个AsyncTask。 The second processes the frame and if it is necessary it should communicate the third AsyncTask to take the picture. 第二个处理帧,如果有必要,它应该传达第三个AsyncTask来拍照。

How can I manage the communication between AsyncTasks already started My idea was to use static objects but it does not seem like an "elegant" solution. 我如何管理已经开始的AsyncTasks之间的通信我的想法是使用静态对象,但它似乎不是一个“优雅”的解决方案。

PS: It would be better if they communicated with objects, but I also accept tips on how to communicate simple messages. PS:如果它们与对象进行通信会更好,但是我也接受有关如何传递简单消息的提示。

Thank you 谢谢

Your approach has several problems that can prove to be hard to tackle: 您的方法存在几个可能难以解决的问题:

1) Mat objects are quite expensive in terms of memory and allocation time, so if you capture several frames per second and keep them in memory while the AsyncTask runs you can end up with OutOfMemory exceptions; 1)Mat对象在内存和分配时间方面非常昂贵,因此,如果您每秒捕获多个帧并将它们保存在内存中,而AsyncTask运行,则可能会出现OutOfMemory异常;

2) On some Android versions all AsyncTasks run on the same thread, meaning that they will actually work sequentially. 2)在某些Android版本上,所有AsyncTasks都在同一线程上运行,这意味着它们实际上将按顺序工作。 On the best case scenario there is a thread pool used for running AsyncTasks, meaning you will have a finite number of running tasks. 在最佳情况下,有一个线程池用于运行AsyncTasks,这意味着您将有数量有限的正在运行的任务。

I would suggest you do all your processing inside onCameraFrame(CvCameraViewFrame inputFrame) . 我建议您在onCameraFrame(CvCameraViewFrame inputFrame)内进行所有处理。 But i guess you already tried that and the processing time is to big to display a preview that has sufficient FPS. 但是我想您已经尝试过了,处理时间很大,无法显示具有足够FPS的预览。

To strictly answer your question, you could use an event bus communication system for the AsyncTasks. 要严格回答您的问题,您可以将事件总线通信系统用于AsyncTasks。 I have used Otto, and it is quite easy to setup and there are plenty of examples( http://square.github.io/otto/ ). 我使用过Otto,并且安装起来非常容易,并且有很多示例( http://square.github.io/otto/ )。 This is a very good example of using Otto ( http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/ ). 这是使用Otto的一个很好的例子( http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/ )。 Just make sure you create a separate class file for each tasks response or you'll get tasks consuming each other's messages. 只需确保为每个任务响应创建一个单独的类文件,否则您将获得使用彼此消息的任务。

However, if you want to continue with your approach I would suggest you use threads and intercommunication. 但是,如果您想继续使用该方法,建议您使用线程和互通。 There are plenty of simple examples around. 周围有很多简单的例子。

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

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