Android AsyncTask Example



















Android how to work with AsyncTask  ?

Android AsyncTask work Threading in android.See this Android Painless Threading .

For Example ,i have AsyncTask which name is MyTask.

MyTask objMyTask = new MyTask();

Now to start AsyncTask call objMyTask.execute();

Rules::

  • The AsyncTask instance must be created in UI thread. 
  • .execute must be invoked on the UI thread.
  • Never call  objMyTask.onPreExecute(), objMyTask.doInBackground() objMyTask.onProgressUpdate() objMyTask.onPostExecute manually.
  • The AsyncTask can be executed only once (an exception will be thrown if a second execution is attempted.)

AsyncTask have Four Main Method...
  1. onPreExecute() 
  2. doInBackground()
  3. onProgressUpdate()
  4. onPostExecute() 

  • onPreExecute-This method is called first when you start AsyncTask using objAsync.execute().And mostly this method is use for initializing dialog(ProgressDialog,CustomDialog) and showing.
  • doInBackground-The main purpose of AsyncTask is accomplished by this method.Any non-UI thread process is running in this method.Such as Rss Feed Reader,Image and video Uploading and Downloading.You cant handle your View in this method.Because this method is non-UI thread.While any background process is running if you want to handle UI therea are  onProgressUpdate method. after completion of process this method send result to OnPostExecute.
  • onProgressUpdate-While backgrounding task is running ,you can handle your UI using this method .Such as status of downloading or uploading task.and this method is called from  doInBackground.Using publishProgress() you can call onProgressUpdate method to update UI while process is running.
  • onPostExecute -This method is called after the background computation finishes.The result of background process in passed in this method as parameters.And now you can dismiss progress dialog ,to indicate that background task is completed.


You can cancel AsyncTask using objAsyncTask.cancel().then you just check in doInBackground,

if (isCancelled()) {
break;
} else {
       //continue...
}


See this Image For more Clear.



9 comments:

  1. Nice Thread Tutorial, i am new in android , its help me a lot ...

    I have got one good links here at androidexample.com

    AsyncroTask Example To Get Server Data

    ReplyDelete
  2. Is it Posiible to call another Async task from the presently working Async Task???

    ReplyDelete
  3. Great tutorial It helped me a lot.Thanks..

    ReplyDelete
  4. Great tutorial! The diagram is very useful for understanding how the AsyncTask class works.

    Thanks!

    ReplyDelete
  5. I really appreciate the way you explained. Thank you very much for explaining in such a constructive way. Often we see experts gives tutorial, but they miss one point clarity-they keep using jargon words. Please keep it up.

    ReplyDelete
  6. Nice post man!! with a clean example... Thanks.. Even this http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html might help... Have a look...

    ReplyDelete
  7. For advanced android concept on async

    ReplyDelete
  8. You have explained the topic very nice. Thanks for sharing a nice article.Visit Nice Java Tutorials

    ReplyDelete

Android Testing App