Thread.Sleep issue android methods and asynctask - android

I am working on app that updates data for every 8 secs and the update was done using Async task. I am using loops to achieve this condition
while(const_val > update_val) {
new Asynctask().execute();
Thread.sleep(8000);
}
const_val will be constant and will be not be changed by any other methods.lets say this value will be 5.update_val will be updated and decremented when Asynctask is called and let's the value will be 10. So , the while loop executes until the condition is true and asynctask ,sleep are called .
When I use above while loop in a general method then UI gets locked and if I use the same loop in another asynctask there was an error saying "Only original thread that created a view hierarchy can touch its view "

You need to change your code to start the AsyncTask and have it provide an update via its onPostExecute() method. By calling Thread.sleep() you are sleeping the main thread (or UI thread) of your app, which is not good. You do not ever want to block the main thread. This article may help you better understand AsyncTask and threading in Android: http://po.st/Cei3m2

I don't think you should use a surrounding loop. Look at this example:
http://javatechig.com/android/progress-notification-in-android-example
the AsyncTask is a private inner class
the onPostExecute updates the UI with a message/cancels the load bar
This way you don't have to loop and the onCreate() can return instantly.

Related

Use ASyncTask onProgressUpdate to update UI

I have an ASyncTask that tries to
find an image on disk
checks if newer online
Then in onPostExecute update the image
I would like to update image (i.e. sync to ui thread) both after checking disk (which is fast, but still to slow to do in e.g. ListView getView) and after checking online.
If I read the help correctly, I can call onProgressUpdate (executed in UI thread) by using publishProgress(Progress...) ... However, can I be sure of the order? I don't care about timing (if I have to wait a little) but I would like to make sure "disk-progress-state" happens before "online-progress-state"
It's entirely up to you to decide when you call publishProgress() and what parameters you pass to it.
If you want to call it twice from your background thread - once after the disk check, and once after network check - then that's fine. To distinguish between the two calls in your onProgressUpdate() just keep track of what state you're in or keep a count of how many times that function has already been called.
The calls on the UI thread will be in the same order as the calls you make to publishProgress() in the background thread.
If I understood you correctly you are asking if executing publishProgress invokes onProgressUpdate in the same sequence. The answer is yes, because it is posting messages via handler to the UI thread.
Here is the code from AsyncTask publishProgress
sHandler.obtainMessage(MESSAGE_POST_PROGRESS, new AsyncTaskResult<Progress>(this, values)).sendToTarget();
AsyncTask runs the methods on the UI Thread by sending Runnables to the main thread Handler. This Handler takes every Runnable in the FIFO queue and run them after another sequentially with no parallelism. Thus if the disk-progress-state happens before online-progress-state, then the order of the publishProgress's is preserved.
The same applies to the order of publishProgress and onPostExecute.

AsyncTask calling main class method, did it work fully in background?

This is my problem..i am calling a method in doInBackground of my AsyncTask class, the method is declared in my main activity class. Does it work in background thread? or i need to write the whole method inside the doInBackground ??
protected String doInBackground(String... params) {
getAllUsersfromServer();
return null;
}
this getAllUsersfromServer(); is declared in the main class. the method download some data from server using REST..
I need to run this whole process in background thread. Does it works?
Does it work in background thread?
AFAIK, Yes it works in background thread.
do I need to write the whole method inside the doInBackground ??
No need to do that.
onPostExecute works very next moment.. even my users information are
still loading
This is the main point, Its just working line by line, when it goes to execute getAllUsersfromServer(); the control goes to execute that method which gets executed in Another Background Thread. [To understand add one log print line below the method call in doInBackground and one in the method's loop and you will see even if the loop doesn't complete the doInBG log will get printed]
This happens because, your method getAllUsersfromServer(); is Void, and the Android takes it as some other independent work to be done and doesn't wait till it gets complete and keep moving to next lines.
Solution :
Just add one return type i.e. boolean getAllUsersfromServer(); and add return statement in the method return true; and in your doInBackground boolean flg = getAllUsersfromServer();
AsyncTask method doInBackground works asynchronously in a separate thread and your main thread remains available to display UI.
However what we usually do is that network processing such as "getAllUsersFromServer" is done using static HttpHelper class or method etc.
Yes it will work in the background thread. check this
I don't know why you would want to do that, but anyhow:
You'll have problems if the activity is destroyed during the async call, like if there's an orientation change. A solution to that would be to make the method static.

One difference between handler and AsyncTask in Android

Why can an AsyncTask perform only one job? For example,
task = new SubAsyncTask(...); // assume the parameter is correct.
task.execute(...) //
task.execute(...)// calling once again, it throws exeception.
But a Handler can continously perform more than one task:
hd = new Handler(...); // assume the parameter is correct
hd.sendMessage(...); //
hd.sendMessage(...);// no exeception is thrown.
Is an AasyncTask object for a one-time job only? If I don't want to create multiple object for similar task, should I choose Handler?
Handler and AsyncTasks are way to implement multithreading with UI/Event Thread.
Handler allows to add messages to the thread which creates it and It also enables you to schedule some runnable to execute at some time in future.
Async task enables you to implement MultiThreading without get Hands dirty into threads. Async Task provides some methods which need to be defined to get your code works. in onPreExecute you can define code, which need to be executed before background processing starts. doInBackground have code which needs to be executed in background, in doInBackground we can send results to multiple times to event thread by publishProgress() method, to notify background processing has been completed we can return results simply. onProgressUpdate() method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update event thread, onPostExecute() method handles results returned by doInBackground method.
So, you dont need to call execute method on AsyncTask multiple TImes, instead you can invoke publishProgress.
Because that is how the class was designed. The idea is: do something with UI (show progress dialog, etc.), do work on background thread and return results, update UI. The Handler is fundamentally different: it lets you post messages, but it does not create a background thread for you. If you don't like how AsyncTask works, build something similar by using threads/executors and handlers.

How to wait for the updated value in an activity

I have an activity that displays a number. The number is obtained from SharedPreferences e.g
int num=pref.getInt(myAsyncTask.MYNUMBER,0);
The SharedPreference is loaded inside an AsyncTask where calculations take place (that involve retrieving data from db and so on). So, OnCreate() of the activity I run the Asynctask to calculate the value. However, the value is not always updated to the latest one when I launch the activity. This might be because Asynctask takes more time to calculate the new value than the the action of displaying the value inside the activity. How can I delay the execution of the method displaying the value until the Asynctask finishes with the process of the new one?
[[EDIT]]
I removed sharedpreferences. I use the onPostExecute() method to get the result. However, the behaviour is similar. The result is returned after the value is displayed so i couldn't see anything at all.
If the calculation doesn't take more than few second, the simplest solution is to use AsyncTask.get(), this method will block UI thread execution and make UI thread waiting for AsyncTask to finish:
public void onCreate(Bundle savedInstanceState) {
... ...
MyAsyncTask myAsyncTask = nee MyAsyncTask();
myAsyncTask.execute();
myAsyncTask.get(); // alternatively you can use AsyncTask.(long timeout, TimeUnit unit)
... ...
}
Bear in mind that it blocks UI thrread execution, if your calculation is longer than 5 seconds, you will probably get ANR exception.
Since you already use AsyncTask, a more reasonable solution is moving execution of the method displaying the value into AsyncTask.onPostExecute(), this is the exactly reason why AsyncTask exist in API to solve this kind if scenario.
Just use listener concept, or just 'pass' your Activity to your asynctask so It can call the main thread.
If the value is not showing as correct at the time you launch the activity you should set the visibility of the View to View.GONE so it doesn't appear, then change the visibility back to visible when you have the result. To update the value you could implement a listener on your activity that gets called by the AsyncTask.
If its going to take a significant time (more than a few seconds) you might want to also use a progress dialog so the user understands that they need to wait.

AsyncTask - How to wait for onPostExecute() to complete before using values it has updated

I have an API in one jar that uses AsyncTask to carry out some work in the background. I need to wait for the result to be returned and then do some more wok with this result.
I signal back to one class in onPostExecute() method and then the result is handled on the UI thread, but the handler needs to be defined as a callback in a different project altogether so I can't just do the work in the onPostExecute.
In the second project I need to wait for the AsyncTask to end AND for the response to be handled, then I need to display the results of the handler to a TextView in an activity in the second project.
Thread.sleep and using a CountDownLatch don't work because the handling is done in the UI thread. Is there any way to do such a thing?
If I understand correctly, you have AsyncTask in one jar and UI that needs to be updated in another jar. If you run them as one application it should not matter where they are located. Since you mentioned that some callback is involved you can just execute this callback in onPostExecute.
The following will be an approximate sequence of events
Activity from jar 2 creates async task and passes callback that knows how to update TextView as parameter to constructor
AsyncTask stores callback in instance variable
Activity executes AsyncTask
AsyncTask in onPostExecute calls method on callback with appropriate parameters
Callback updates TextView

Categories

Resources