Sunday, April 15, 2012

Asynchronous task, Updating UI from background in android

Posted by Blogger Name. Category: ,

As all we know in android there are two ways to perform background task such as downloading image and other task that include long operation
  • Using Thread 
  • Asynchronous Task                          
Using Thread, there is one dis-advantage that we can not update user interface other wise we will get Looper.loop() exception

So its efficient to use Asynchronous task.
An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called ParamsProgress and Result, and 4 steps, called onPreExecutedoInBackgroundonProgressUpdate and onPostExecute.


Let take one example of downloading images from URL's Array.So body of Asynchronous task 's body will be like this


 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     
protected Long doInBackground(URL... urls) {
         
int count = urls.length;
         
long totalSize = 0;
         
for (int i = 0; i < count; i++) {
             totalSize
+= Downloader.downloadFile(urls[i]);
             publishProgress
((int) ((i / (float) count) * 100));
         
}
         
return totalSize;
     
}

     
protected void onProgressUpdate(Integer... progress) {
         setProgressPercent
(progress[0]);
     
}

     
protected void onPostExecute(Long result) {
         showDialog
("Downloaded " + result + " bytes");
     
}
 
}
 
    Like Thread we can create asynchronous task like this

 new DownloadFilesTask().execute(url1, url2, url3);
    Now asynchronous has three generic type.Let have a look on their requirement 
    


  • Params, the type of the parameters sent to the task upon execution.
  • Progress, the type of the progress units published during the background computation.
  • Result, the type of the result of the background computation.

  •     
    But its not necessary to use all type.you can set like this if you do not want to use 

    private class MyTask extends AsyncTask<Void, Void, Void> { ... }
     Now it has four step -

    1)  onPreexecute() -Before it start executing it call this method .It call before it goes to perform task in background so we can display a ProgressDialog here )doInBackground() - perform long background task here

    3)onProgressUpdate() - Update your progress of dialog to show to user

    4)onPostExecute() - Post or show your result here .Dismiss progress dialog  box here
    There are a few threading rules that must be followed for this class to work properly:
    • 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)doInBackground(Params...)onProgressUpdate(Progress...) manually.
    • The task can be executed only once (an exception will be thrown if a second execution is attempted.)

    0 comments:

    Post a Comment

    ◄ Posting Baru Posting Lama ►