In one of my project I need to call asyncTask in my getView method of custom adapter.
So ther is multiple asynctask request are in queue.
By default the asynctask work as first in first out.That is first request will process first then it will goes to next one. What i need is last in first out.
How can I handle asyncTask to work as mentioned above?
Also like to whether there is any method to get the count of pending asyncTask request.Searched a lot couldnt find a solution.
Related
I have several AsyncTask in my Project to grab data from some given apis.
I followed the following steps.
1) execute an Async Task and try to grab datas from there.
2) check conditions for internet and server down
3) if any issue in api or internet or server then show dialog [custom from self made class]
4) dismiss button for canceling the dialog and go back to the working stage of `app may be even by closing the activity`
My problem is I want to keep a Button "Retry" such that it shall be re executing the AsyncTask.
I searched of passing AsyncTask but it seemed worthless as I concluded AsyncTask cannot be passed. And so I am forced to repeat same code cancel(true) for various times
It would be very useful if anyone could give me solution for it with this code re-use concept.
You cannot "retry" the same AsyncTask object - you can only call AsyncTask#execute() once. However, you could create a new AsyncTask instance when the user decides to retry the download task.
Solved The problem by using a retry button that creates the new object of class implementing Async-task and executing that new object.
I am performing an activity that takes long time(offline). I am sending and receiving(reading) sms in my app for this i am using a count down timer class which has a listener onTick() and onFinish(). in the onTick() listener i am checking if there is any new message and if there is any new message from the expected message it will stop the timer and display it on my screen. Everything seems to be okay. the problem arises when i leave and re-enter the app or change the orientation.
While working with network calls robospice takes care of all these activity transition problems. So I want to solve the above issue using robospice. I have seen the robospice offline example in the github. But there LoadDataFromNetwork() doesn't handle any listeners. It just performs a prolonged task.
If I had correlated the offline example with my problem then what I have to do is just call the spiceManager.execute() method inside my onTick() listener and the loadDataFromNetwork() will just read the message from my inbox and check if there is any message of desired kind and return that to the requestListeners. But that's not what I want. I want robospice to moderate the complete timer class.
I couldn't get how to fire robospice requestlisteners only when I read the specific required message as the reading activity is performed in timer ontick() Listener.
Is it actually possible with robospice to do like this ?? If not please give me a solution to deal with the actual problem mentioned in the first paragraph.
If you're switching to using RoboSpice then there doesn't seem to be any need for a CountDownTimer, you could implement your own polling in loadDataFromNetwork. This will be executed in a worker thread in a Service so blocking this thread will not affect your UI.
So a simple for loop with sleeps should get the same result.
One thing to be wary of is the number of threads your SpiceService is configured to work with. If this is a very long running task and you're using the default single worker thread then I believe you'll block subsequent SpiceRequests. If you haven't already subclassed the SpiceService class you can do so and override the getThreadCount method to return an int greater than 1.
I have a activity that show one listview. In the activity I have a one AsyncTask (named here of AsListView) to get values from internet and fill some informations in each item of the listview. Work fine.
Now I created a button in ActionBar to show one image from streetview. To do this I have implemented another AsyncTask (named here of AsImage) to get image from google and show in a DialogFragment, but is necessary wait the execution of all AsListView Threads. It spends long time depending of the number of items in the list.
To execute AsImage rapidily, I cancel all AsListView tasks, but it's not good for me (user loss informations). The ideal soluction is set AsListView tasks to wait while AsImage execute. When AsImage finish I set AsListView tasks to continue execution. But I know that is not possible handle the control of execution of AsynkTasks.....
Some solution?
You can try to synchronize the AsyncTasks using a CountDownLatch.
Otherwise if you want you can use Threads instead of AsyncTasks and set their priorities, but there is a reason android made the AsyncTask class so I recommend you use it.
After I've downloaded a list of objects from a rest service, obviously in an AsyncTask, I set them into a custom BaseAdapter. When I call again the service to load more data and add them to the List of object, and call the notifyDataSetChanged() the ListView block for a sec or two.
I've tried to move the add and notify into another AsyncTask but since I'm modifying the UI this raised an exception.
I've tried also to change the addAll with a loop where I add an item at time and call the notifyDataSetChanged() everytime but with no success.
Which is the best practice in this case?
Sorry for no code but I'm from my phone, this thing really puzzles me.
How many times you are call notifyDataSetChanged()? You need to call it once, after you add all data. If you show code of your AsyncTask class and how do you use it, I will tell more.
So, I'm working on a barcode decoder, which once we have the barcode goes to multiples API over the internet to decode what was just scanned.
The thing is that I have to link some XML parsing together, and I don't know if I'm doing it right.
So, once the barcode is scanned, my program calls an ASyncTask which goes over an API to retrieve the product name. Once it has the name, I want it to call another ASyncTask. I know this is possible by instantiating an ASyncTaks in the onPostExecute() of the other but, I think this is wrong, because it's like boxes within boxes.
So isn't it possible/better to instantiate my second ASyncTask inside my main Activity, and make it wait until my first ASyncTask is finished ?
(english isn't my primary language, I hope I made myself clear).
I think it's absolutely legitimate to start the second AsyncTask in the onPostExecute of the first AsyncTask, Mixing both operations is a bad logical idea, As "The Offspring" said - "You've gotta keep 'em separated"
If you don't want it to be directly inside the onPostExecute itself, set a handler to execute it in the activity and call this handler from onPostExecute.
And last thing - If you have a lot of logic - move it to a separate file, don't keep it all at the same file.
In situations like this it might be better to batch long running operations together in one AsyncTask.
Another option is to use the Loaders API, this makes chaining tasks much easier http://developer.android.com/guide/topics/fundamentals/loaders.html
You can go for another approach if you are facing often a situation like this. That is to merge requests and operations inside of runnables/callables and to manage them separately within say a queue for instance.
Here is a nice approach.
http://ugiagonzalez.com/2012/07/02/theres-life-after-asynctasks-in-android/