我正在尝试找到一些简单的方法来在非 UI 线程的线程上对任务进行排队。
我有一个 BroadcastReceiver,每次拍摄图像时都会响应 Intent,并在每次发生这种情况时通知我的服务。该服务必须进行一些处理(缩放和旋转图像),我想在新线程上执行此操作(该服务与我的主 Activity 在同一线程上运行)。所以我一直在寻找 AsyncTask 。
我的问题是,如果在完全处理前一张图像之前拍摄下一张图像,我将同时运行两个 AsyncTasks,而我不希望这样。我想将下一个处理排队,直到第一个处理完成。
所以基本上我想要两个线程。一个是标准的 UI 线程,另一个是我的工作线程,我可以在其中执行一个任务队列。
最好的方法是什么?
最佳答案
I have a BroadcastReceiver that responds to Intents each time an image is taken, and notifies my Service each time that happens. The Service will have to do some processing (scaling and rotating an image) and I would like to do this on a new thread (The service is running on the same thread as my main Activity). So I've been looking at AsyncTask for that.
IntentService
是解决此类问题的一个很好的解决方案,因为它将工作队列与 Service
中的后台线程结合在一起。您只需重写 onHandleIntent()
即可完成实际工作。
My problem is that if the next image is taken BEFORE the previous has been fully processed, I will have two AsyncTasks running simultaneously, and I don't want that. I would like to queue the processing of the next one until the first one is done.
IntentService
一次只会处理一个 Intent
。
关于android - 如何在 Android 工作线程上对任务进行排队?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058229/