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.
Related
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.
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 file downloading android application.Here file is downloaded when user click on listview items.It works fine.
Now,I want to cancel current file download and start next file while user taps on other items and another download is already progressing and show user confirmation before canceling the download task.
How can i do this?
Thanks in Advance
First, you must check in your AsyncTask
// in doInBackground, check if asynctask is canceled manually
if (isCancelled()) break;
In your activiy, whenever you try to cancel, just cancel AsyncTask manually, and of course, start a new one to download another file!
mDownloadingTask.cancel(true);
Simply , you store your Asynctask to a variable:
mDownloadingTask = new Asynctask(){...};
mDownloadingTask.execute();
When you want to stop this task and start new one:
//close the current task if it # null
mDownloadingTask.cancel(true);
mDownloadingTask = null;
//Start new one
mDownloadingTask = new Asynctask(){...};
mDownloadingTask.execute();
Cancel(false) sets a flag, which you can check in doInBackGround() and stop your code from doing what its doing. cancel(true) does the same except it also interrupts the thread. See more info about interrupts here.
This is all good when you have a looped code in your task. Every iteration of the loop, you can check isCancelled().
But as in your question, some tasks like downloading a file do not use loops, perhaps you are just calling an external API method, so here, you are on the mercy of this blocking method. If well designed, it will throw an exception when interrupted, and the thread will terminate.
Also, have a look at a better implementation, SafeAsyncTask
After I press a button, Android parse a JSON file and pick the info it needs. Until yesterday, I was using an external library created by a user and it worked perfectly. But now, I don't want to depend on him, so I've searching info about Google's GSON. I've implemented this library with no problem, but now, after pressing the button that opens a new activity there's a delay.
This delay is due to the connection and parsing that are done before the activity shows.
How can I force the app to wait the Internet connection until de Activity is shown? It's a lil bit uncomfortable because after pressing the button, it seems that the app has frozen, but after all data is loaded, the new activity appears.
Thank you in advance!
Use AsyncTask or Handler for network operations. Do never put "long time" operations into the UI thread
Use asyncTask.. and if server communication is a success then show the new Activity or else exit..
it is possibl;e via Handler and AsyncTask see this How to set delay in Android onClick function you will get how to use Handler and see this for Asynctask https://stackoverflow.com/questions/7644567/need-a-simple-example-for-android-asynctask
Okay, I've read the Android documentation and been perusing article after article on ASyncTask and just don't understand how to get information back from my external ASyncTask class. This runs fine:
myASyncTask = new MyASyncTask();
myASyncTask.execute(myParam);
...and I fully expect the task to complete but how do I get anything back from it? The documentation says that onPostExecute is invoked on the UI thread but it also says to not call onPostExecute manually?!? How do I get data back from my ASyncTask object???
I've got it to work fine when I create my ASyncTask as an inner class but I'd rather this task be external so I can call it from different Activitys.
If you read the documentation you can use get method to get the result , and it waits till the task is done .
You can also use getStatus to get the current status of the task , assuming it publishes it .