简体   繁体   中英

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. The second processes the frame and if it is necessary it should communicate the third AsyncTask to take the picture.

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.

PS: It would be better if they communicated with objects, but I also accept tips on how to communicate simple messages.

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;

2) On some Android versions all AsyncTasks run on the same thread, meaning that they will actually work sequentially. On the best case scenario there is a thread pool used for running AsyncTasks, meaning you will have a finite number of running tasks.

I would suggest you do all your processing inside 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.

To strictly answer your question, you could use an event bus communication system for the AsyncTasks. I have used Otto, and it is quite easy to setup and there are plenty of examples( 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/ ). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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