简体   繁体   中英

How to queue tasks on a worker thread in Android?

I'm trying to find some simple way of queueing tasks on a thread that is NOT the UI-thread.

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.

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.

So basically I want two threads. One is the standard UI-thread, and another one that is my worker thread where i can have a queue of task to perform.

What would be the best approach?

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 is a fine solution for this sort of problem, as it combines a work queue with a background thread in a Service . You just override onHandleIntent() to do your actual work.

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 will only process one Intent at a time.

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