views:

108

answers:

2
+1  Q: 

Android Asynk Task

Hi, is a good practice to have an Asynk task with a loop like this inside?

while (true) { 
check queue and request API

Because i need to wait for my Activities and service needs to comunicate with the APi.

Thanks

A: 

I am assuming the "queue" is a Java queue, perhaps a LinkedBlockingQueue. If so, that queue is unnecessary, as AsyncTask has a queue to go along with its thread pool.

So, the question is: what triggers things to go in the queue?

If the trigger is a user event (e.g., menu choice, button push), just have it launch an AsyncTask, or have it have a Service launch an AsyncTask. That work will be queued up by the AsyncTask system.

If the trigger is the passage of time (e.g., we want to check an external server every 10 minutes), I would use AlarmManager and consider switching from AsyncTask and a Service to using an IntentService.

CommonsWare
A: 

Hi again,

I have a priority queue in order to select first the important calls to the API.

My program have two services:

  • One calls the API when a message is added to the queue. The call to the api is made by an Asinc Task in this way:

    messages.add(request);  
    new DownloadApiTask().execute();
    
  • The other service is updating the local database. For that, i have a loop in which i call the first service in order to get data from the API. The basic structure is:

    while ihave data to updload

    mFirstService.putMessage(request).

Fine, the problem is i have a rejected Execution Exception every X calls, I think it can be because i invoke the asinc task every time i take a message.

For that, i was thinking in force to the asinck task to check the queue instead executing it.

I hope you can understand my problem.

Thanks

xger86x