Can we create a common asynck task for multiple use.? If yes so please tell me, actually I am new in Android. Please explain how can we do the same.?
Thank you.
Can we create a common asynck task for multiple use.? If yes so please tell me, actually I am new in Android. Please explain how can we do the same.?
Thank you.
Can we create a common asynck task for multiple use.?
AsyncTask
instances can only be used one time.
Instead, just call your task like new MyAsyncTask().execute("");
The goal of the AsyncTask is to take care of thread management for you and you should not worry about the threading mechanisms.
The Android Platform handles the pool of threads to manage the asynchronous operations. AsyncTasks are like consumables.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Source: should-you-create-new-async-tasks-for-every-different-call-or-use-the-same-one.
By using generic Asynck task, can we use one asynck task for multiple purpose..??
Check this Post - common-class-for-asynctask-in-android.
Can we create a common asynck task for multiple use.?
it depends on what kinds of task you want to perform. If you want to:
then you should use a common AsyncTask
class.
If the results and functionality are different then you should create a new AsyncTask
for such types of calls.
For example I needed to call a web service to obtain result in JSON then I created a common class (extends AsyncTask
) which took the target URL as parameter and returned JSONObject
.
Here is the code
public class CallService extends AsyncTask<String, Void, String> {
Context context;
ServiceCallback callback;
ProgressDialog dialog;
List<BasicNameValuePair> data;
public CallService(Context context, ServiceCallback back,List<BasicNameValuePair> data) {
this.context = context;
this.callback = back;
this.data = data;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if(dialog==null || !dialog.isShowing())
{
dialog = new ProgressDialog(context,AlertDialog.THEME_HOLO_LIGHT);
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
}
@Override
protected String doInBackground(String... params) {
return CommonClass.getJSONFromUrl(params[0], data);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (dialog.isShowing())
dialog.dismiss();
callback.callbackCall(result);
}
}
public interface ServiceCallback {
public void callbackCall(String response);
}
now when I have to call the service I use:
public class Activity1 extends Activity implements ServiceCallback {
@Override
public void callbackCall(String response){
//process and upate data
}
}
Remember that you need to implement the interface ServiceCallback
if you want to use the Common AsyncTask
class in an Activity.
Now this is how I call the AsyncTask
List<BasicNameValuePair> values = new ArrayList<BasicNameValuePair>();
values.add(new BasicNameValuePair("value1",value1));
values.add(new BasicNameValuePair("value2",value2));
new CallService(Activity1.this, Activity1.this, values)
.execute("http://www.example.com/someservice");