Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

some kind of queue for asynctask

Hello I have ListView with list of files. i click item and start to download this file in asynctask. then i click another one and it must be put in queue, wait for that file and start ot download after it finishes. i can make some class that will hold all clicked links, and pass it to asynctask downloading part? and than somehow process them. but want to know is it the right way? any links of sugestions? thanks

like image 466
SERG Avatar asked Apr 14 '11 05:04

SERG


People also ask

How many methods are there for AsyncTask class?

Asynchronous tasks are divided into three generic types: Params, Progress, & Result and four steps: onPreExecute, doInBackground, onProgressUpdate, & onPostExecute.

What are the three generic types of an AsyncTask?

An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

Which methods are run on the main UI thread AsyncTask?

onPreExecute: This is the first method that runs when an asyncTask is executed. After this, doInBackground is executed. This method runs on the UI thread and is mainly used for instantiating the UI.

How many threads are there in AsyncTask in Android?

In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutor will attempt to run the AsyncTask s on these 5 threads. If you create more than 5 AsyncTask s, it may either queue them or create new threads (but only up to 128).


2 Answers

If you're set on using AsyncTask then, yeah, hold your clicked links and kick off new tasks when appropriate. You should note that AsyncTask is like the 'pocket knife' for threading in Android apps.

If you really need to manage a bunch of background tasks, and it sounds like you do, take a look at ThreadPoolExecutor. You get a lot of flexibility. BlockingQueue ThreadPoolExecutor More Info Example

like image 178
Brandon Avatar answered Oct 02 '22 18:10

Brandon


Take a look at HandlerThread and the Handler class. You need one handler to pass tasks to the background HandlerThread and another for the UI thread to pass results back to the UI

like image 27
Philip Pearl Avatar answered Oct 02 '22 18:10

Philip Pearl