Hi \I keep getting an error for my Async onCreate.
This is the code, and the logcat trace. Any feedback appreciated.
The splash screen runs then the app stops once it starts the homescreen
public class HomeActivity extends ListActivity {
You are not supposed to manipulate views in doInBackground of your AsyncTask.
doInBackground() runs on the background thread. So you cannot make changes to the ui on the background thread. You need to make changes or update ui on the ui thread.
http://developer.android.com/reference/android/os/AsyncTask.html
When an asynchronous task is executed, the task goes through 4 steps:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
Use runonuithread or update ui in onPostExecute() depending on the result returned in doInbackground().
Check the link below
Doing UI task in doinbackground() in Android
Related
At first, this is for a step counter.
My initial structure is a service keeps logging step counter value to database.
Then a async task keeps updating the value shown to user when the app is visible to user.
I planed to create a thread to periodically call the async task.
However, after digging into the official document, "async task should be created and invoked within UI thread".
The conflict now is UI thread should not be blocked vs calling async task periodically.
Or there is else a better way to implement?
Thanks for any input.
You need to derive from AsyncTask inside your UI class (service or activity).
Inside your AsyncTask (as described here) there is doInBackground which runs asynchronously and there is onPostExecute which runs inside UI thread after your asynchronous task is over.
Just put your DB operation inside doInBackground and put something like this inside onPostExecute
MyServiceClass.this.RunNextTask();
The RunNextTask method in your UI class could use the same AsyncTask to launch the next task.
Say there is an AsyncTask that is triggered from a UI button, and the button click triggers execute() to start the task. What happens if the task is in the middle of execution, and user calls execute() again?
I know this can be prevented by checking AsyncTask.getStatus(), but what happens if I don't want to check that?
Due to AsyncTask doc:
There are a few threading rules that must be followed for this class to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), oInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Hope this help!
My App contains a function that takes time to load ( parsing files).
THe function is called at multiple user case, i.e. from multiple user triggered condition.
Besides, it is called when onCreate is called.
In simple word, the flow is:
User click/OnCreate trigger
Function to parse file
Post to windows
Other postprocessing
I hope the user can click cancel to stop parsing files.
I tried to use asynctask. I know I can put the function to onPostExecute.
But I assume onPostExecute is just for dismiss progress dialog. Or I have to move a lot of codes ( for different cases) to it. Not a good idea.
I do not suppose user to do anything during parsing files.
So, what is the best way to do so? Despite I know it is not good, I think i have to occupy the UI thread.
In simple word, I want to wait for "parsing files", but i do not want to occupy the UI thread, so user can click cancel.
update:
I tried. however, there is a problem:
I use asynctask. I called:
mTask = new YourAsyncTask().execute();
YourAsyncTask.get(); // this force to wait for YourAsyncTask to return.
DoSomethingBaseOnAsyncTaskResult();
YourAsyncTask.get() hold the UI thread. So, there is not loading dialog, and user cannot click cancel from the dialog. It seems I have to move every line after
mTask = new YourAsyncTask().execute();
to
OnPostExecute()
which i did not prefer to do so because DoSomethingBaseOnAsyncTaskResult() can be very different based on the return result. or else, it becomes do everything in YourAsyncTask()
AsyncTasks should ideally be used for short operations (a few seconds at the most.)
When an asynchronous task is executed, the task goes through 4 steps:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time.This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
CODING
To start an Async task
mTask = new YourAsyncTask().execute();
and to cancel that task
mTask.cancel(true);
More detail is available here
In order to use the AsyncTask API, one has to follow the steps described below:
Create a class which extends AsyncTask.
Fill in the generic types available as generics in the class for:
the task execution array parameters
progress array parameters
result array parameters
Implement the method doInBackground(Parameters... parameters). This
method must execute the job which is supposed to be quite demanding.
Optionally, one can implement methods for:
cancelling the task - onCancelled(...)
executing tasks before the demanding task - onPreExecute(...)
reporting progress - onProgressUpdate(...)
executing activities after the demanding task is finished
-onPostExecute(...).
For the down voters,it would be better if you could provide a working solution,not every question need to have a code attached with it,if one is not clear with the concepts how can u expect him to provide you with the code he played with??
This is basically a conceptual question,i tried reading docs but still i couldn't get a better understanding of the topic. i am not sure how i should use async task....
i have used the async task before for displaying an image from internet,
but i am still confused how it works.I know 3 of its functions that are used commonly.
i.e
1.onPreExecute ()
2.doinBackground()
3.onPostExecute()
now i am confused that if i have to populate a list how should it be done??
I know The populating part should be done in the doinbackground(),but after that should i return the result (from the background),after the whole list has been populated, to
onPostExecute() and expect that the list will be loaded on the listview asynchronously
or should i return the result in parts(say a new item has been added to the list,send it to the onpostexecute immediately without waiting for the whole list to be generated, to be displayed and repeat the iteration ) to the onpostExecute()??and manage the lazy load ourselves by doing so?
well.. why don't you see this below android API information..
The 4 steps
When an asynchronous task is executed, the task goes through 4 steps:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
onPreExecute() method is used when starting asynktask function.. typically in this method, someone use progress dialog..
and doInBackground() methos is used when progress dialog is running. in this method you can implement your job(function) that you want. I think this part is the most important point among methods of this asynktask class
and onPostExecute() method is typically used when finished background job.. in order to deliver some kind of data or result.. to View
I am not sure if this question has been asked on SO before. When executing an async task using myTask.execute(); what method runs at the very beginning. I am following this tutorial: http://mobiforge.com/developing/story/consuming-json-services-android-apps for consuming JSON services and the author is using new ReadWeatherJSONFeedTask()
.execute() along with some parameters. I am confused as to which method runs first and how the parameter are being passed along to get the result.
Can anyone help me.
Thanks.!
Parameters that the asyncTask needs, are declared while defining the class that extends asyncTask.. And the sequence of execution of methods is:
onPreExecute() ---it runs on the UI thread
doInBackground() and onProgressUpdate() -- they run on worker thread
and onPostExecute() --- runs on UI thread.
According to Android API Reference AsyncTask,
When an asynchronous task is executed, the task goes through 4 steps:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance
by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used
to perform background computation that can take a long time. The
parameters of the asynchronous task are passed to this step. The
result of the computation must be returned by this step and will be
passed back to the last step. This step can also use
publishProgress(Progress...) to publish one or more units of progress.
These values are published on the UI thread, in the
onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is
undefined. This method is used to display any form of progress in the
user interface while the background computation is still executing.
For instance, it can be used to animate a progress bar or show logs in
a text field.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background
computation is passed to this step as a parameter.
As this explains onPreExecute() on Ui Thread is executed at the very beginning.
This Guide might help you with more help in this regard.