Pages

Wednesday 31 July 2013

AsyncTask in android

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

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 Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

Read this fully more about AsyncTask

Coding:

public class Login extends Activity {

 LinearLayout Signin;

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);


Signin = (LinearLayout) findViewById(R.id.signin_ll);

Signin.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
// TODO Auto-generated method stub

                  myasynctask MAT = new myasynctask();
   MAT.execute();

   }
});

}

 class myasynctask extends AsyncTask<String, Void, String> {

       @Override
protected String doInBackground(String... params) {
   // TODO Auto-generated method stub
           //Background process like downloading, webservices etc...
           Log.d("doInBackground", "backgroundprocess is ongoing");
          return value;
}

        @Override
public void onPostExecute(String result) {
   // TODO Auto-generated method stub
             Log.d("PostExecute", "after finished the backgroundprocess");
   super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
   // TODO Auto-generated method stub
            Log.d("PreExecute", "before going to start the backgroundprocess");
   super.onPreExecute();
}
}
}

No comments:

Post a Comment