Showing posts with label Background Task. Show all posts
Showing posts with label Background Task. Show all posts

Friday, January 9, 2015

AsyncTask in Android

AsyncTask in Android:

AsyncTask in Android
AsyncTask in Android


Asynctask  in android use for network operation like upload data  and get data from server means "Network Related Task" If without use of AsyncTask we do network related task then "RunOnUIException "  error Occur.

Create AsyncTask  by extend Async , Params,Progress and Result  can be chnage according to need, likes if need String Type data then change String.

AsyncTask method  onPreExecutedoInBackgroundonProgressUpdate and onPostExecute and for acess UI mean without network related task like set value in textbox or show progress dialog etc. onPreExecute and onPostExecute Only for UI. and for "Network Related Task" use doInBackground. method.

for short operation we use AsyncTask and if need large data then use  ExecutorThreadPoolExecutor and FutureTask. easy manage UI and Background related work in AsyncTask but In case of Thread for UI we need runOnUiThread method or handler.

 Create AsyncTask:

                              class AstncTask extends AsyncTask {
                                      @Override
protected void onPreExecute() {
// TODO Auto-generated method stub
                                           super.onPreExecute();
                                          // For UI  
 }

                                           @Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
                                               //"Network Related Task"
}

                                            @Override
                                              protected void onPostExecute(String result) { 
                                               super.onPostExecute(result);
                                               //after doInBackground it call 
                                                 // UI   related work
                                              }
}

Call AsyncTask

new AsyncTask().execute();

visithttp://invokecode.com/  for More Tutorials 

if any query related this tutorial  then please comments !!

Thank You !!