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
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.
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
Related
I am working on an app and want to send some data to server. I was able to send the data to server when the updates that I was sending to server were periodic.
But now Its upon user interaction. I have to send 1 or more calls to server. I cant skip and omit any call towards server. Just suppose the following case:
Case:
Let say I have a button on its click I launch a asyntask that put some data on server and let suppose it takes 5 to 6 seconds. But I want to trigger multiple asyntask , if user press the button multiple time. Let suppose he presses the button 7 time in a row. so the 7 AsyncTask must run in a queue one after an other.
I have heard about schedular and all others but I am unable to understand how to work with them using asynctask as well.
Possible solution : I can disable button until and unless first Async task is not completed, But I really do not want to do it, because under the requirements and needs I have to call async task as many times as user presses the button.
please help me, any source code will be appreciated.
Note: With asynctask I have nothing to update the UI.
Update :
under my need I want to queue the asynctask with specific data and it must automatically run when the previous task is completed and if user exit the application it should continue running until and unless it completes the execution of all queued asynctask.
Let suppose he presses the button 7 time in a row. so the 7 AsyncTask must run in a queue one after an other.
Depending on what API your app target you may simply need to do nothing special. Official documentation reads:
Order of execution
When first introduced, AsyncTasks were executed
serially on a single background thread. Starting with DONUT, this was
changed to a pool of threads allowing multiple tasks to operate in
parallel. Starting with HONEYCOMB, tasks are executed on a single
thread to avoid common application errors caused by parallel execution
therefore starting from Android 3.0 execute() is all you need really, which means unless you support ancient versions of the platform you should be seeing serial execution by default.
Alternatively, you can drop AsyncTask in favor of IntentService which are executed one after another.
I am working on JOB queue. Here the scenario is i am storing 3 api calls in job queue. The first API is having more information in json. but last two is having less information. According to the priority when network is available it is calling those apis one bye one in right order. but as the first api is having more information it is taking more time to hit the server so it is reaching the server at last. Is there any way where after hitting the server we can able to run the rest APIs?
To manage your network request one after other you can Use AsyncTask.
AsyncTask have 3 main method i.e
1)onPreExecute - can perform any Ui update while calling network request.
2)doInBackground - which run in background thread not on main thread.
3)onPostExecute - after doinbackground process onPostExecute will call.
like this you can call your request when first request get completed ,So on first onPostExecute , call second request and so On.
I hope like this you can manage your network request in serial way. Thanks :)
One feature of my application is to retrieve live data (JSON object) every 2 sec and display it (only while app is in foreground). I am executing an async task for every 2 sec. But this is making the app slow. I have searched for alternative, but i only got C2DM option. I can't use it because of server limitations. Could you please tell me an alternative or effective way for polling?
One option, if you have control of your server, is that you can switch to something like Comet (long-held http requests) to avoid the necessity of ongoing polling requests.
change the execution of the async task from every two sec to after getting the response for previous update you can initiate it in onpostexecute function... this will make your ui faster
also try using gzip so that the data gets transferred faster do not pool async task
your ui is getting slower as an async task is shot up before the previous one ore ones have completed
I'm using an IntentService to perform background tasks, such as updating data from a remote server.
In some cases, the same request can be queued multiple times by the user, but I only want to execute it once (there's no point in updating the data from the server twice in a row).
Is there a simple way to do this using an IntentService, or should I just use a standard Service?
Is there a simple way to do this using an IntentService
Unfortunately, no. The Handler queue used by the IntentService is not visible through the SDK and does not have public methods to let you inspect its contents, anyway.
should I just use a standard Service?
Probably. You could try to keep your own parallel copy of the work queue, flagging duplicates and ignoring them in your onHandleIntent(), but making sure you are always in sync with the real internal queue might get icky.
I am in the middle of developing an android application and I have stumbled across something that I don't really know the best way to solve.
What I wwant to achieve is, when a user logs into the application, I want to start a thread if the device is connected to a network(what kind of network doesn't matter)
The thread should perform an action every 10 minutes.
What this thread needs to do is, loop trough a list, a queue to be more exact.
This queue will have objects, and based on the objects in the queue when there is a connection available, execute.
The queue will be filled trough the flow of the application.
For example filling in a questionary.
The answers need to be synched to the server. Every question can include pictures takebn from the camera etc, so I want to save certain data as an object, put them in a queue, and have a thread handle the http requests. This way the UI won't be blocked. It's of great importance to sync whenever possible.
What I want to avoid is having another process run aside from my own APP. That's why I haven't used a service. Or do I missunderstand the concept of services as a whole?
Are there specific queue objects or lists?
I want to loop trough the queue list that can be filled at anytime while the program is alive, with a thread.sleep like method when the list is completely empty.
Please leave me hints and tips on what way to go with this.
A service isn't it's own process... from the Documentation: "A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of."
A service really is the best choice for what you're talking about. You spawn your own thread in the service that then does the following: check your queue for objects and send any to the server (since you're already not on the UI thread, you can do this without spawning yet another thread if you want). If the queue is empty, use a Timer to schedule another invocation of your upload method.