Last Updated: January 19, 2015

AsyncTask Android Example

Introduction

Asynctask(Asynchronous task) in android enable us to perform the task in Background thread giving us the way to separate our tedious work from UI Thread and publish our work back to UI thread after our work get completed.




It has following method(listed in sequence of execution):
    • onPreExceute
    • doInBackGround
    • publishProgress
    • onProgressUpdate
    • onPostExceute
    Note:  We can't access any android widgets (button, progressDialog etc.) elements in       doInBackground. So for updating our work status we publish the progress from doInBackground.

    Android ExampleAsynctask<Params,Progress,Result>


    class AsyncTaskExample extends AsyncTask<Void, Integer, String> {
        
        private  final String TAG = AsyncTaskExample.class.getName();
        
        protected void onPreExecute(){
            Log.d(TAG, "On preExceute...");
        }
        
        protected String doInBackground(Void...arg0) {
            
            Log.d(TAG, "On doInBackground...");
            
            for(int i = 0; i<5; i++){
                Integer in = new Integer(i);
                publishProgress(i);
            }
            
            return "You are at PostExecute";}
        
        protected void onProgressUpdate(Integer...a){
            Log.d(TAG,"You are in progress update ... " + a[0]);
        }
        
        protected void onPostExecute(String result) {
            Log.d(TAG,result); 
        }
    }



    Call in your activity(Any):

     new AsyncTaskExample().execute() - In Our example
     new AsyncTaskExample().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); - for serial task execute.  
     new AsyncTaskExample().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)- For Parallel task.  
    

    Output:









    2 comments :

    1. The goal of our Academy writing service is to set the quality standard on the market and to provide customers with an original work of high quality is essay pro legit. Online custom essay writing services provided by our custom writing company are aimed at your academic success.

      ReplyDelete
    2. This platform has your back when you have been to bodily casinos and wish to play basic slots. However, the winning might not be as excessive as many different free slot video games. Since they offer bonuses that require slot machines, you’ll never 파라오 카지노 see this web site run out of them. Gambling can be fun, however it’s designed to part you out of your hard-earned cash. If you’re going to hit the tables, stick with blackjack, aka twenty-one.

      ReplyDelete

    Your comments are valuable for us !!!