I
had to download data from server and bind it with the ListView.I
studied about this and finally used AsyncTask
for background processing.I'm going to post some code and it's
description about Android
background processing.
AsyncTask
AsyncTask
is designed to perform background processing and publish result on
the UI thread.It is a helper class around Thread
and Handler
and does not constitute a generic threading framework.AsyncTask is
defined by three generic types called Params,Progress
and Result.And
it has four callback methods called
onPreExecute(),onPostExecute(),doInBackground()
and onProgressUpdate().
public Load extends AsyncTask<Params,Progress,Result>
{
}
AsyncTask's generic types
Params:-It's a parameters send to the task upon execution.
Progress:-This parameter shows progress units during background processing.
Result:-It return type of result of the background processing.
If you don't require it's type then you can just use Void instead of it's generic types.
public Load extends AsyncTask<Void,Void,Void>
{
}
AsyncTask's Callback methods
onPreExecute():-It invoke on the UI thread before the task execution.Generally we show ProgressDialog here if we want to show progress Dialog till background processing is completed.
onPostExecute():-It invoke on the UI thread after the task execution.Generally we dismiss ProgressDialog here if we want to dismiss Dialog when background processing is completed.
doInBackground():-It invoke on the background thread immediately after onPreExecute() execute.We put here code that we have to execute on the background thread like image downloading etc.
onProgressUpdate():-It invoke on the UI thread. This method is used to display any form of progress in the user interface while the background processing is still executing.
We
can also cancel task by calling cancel(true).
Example
of Android background processing with AsyncTask.
public
class Load extends AsyncTask<Void, Void, Void> {
Context context;
ProgressDialog progressDialog;
public LoadLatestLive(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
/*put here code that you have to execete in the background thread*/
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "", "Loading");
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
}
}
Context context;
ProgressDialog progressDialog;
public LoadLatestLive(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
/*put here code that you have to execete in the background thread*/
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "", "Loading");
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
}
}
Comments
Post a Comment