3

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.

user3816185
  • 43
  • 1
  • 6

2 Answers2

1

Can we create a common asynck task for multiple use.?

  • You should create a new AsncTask for each call.

AsyncTask instances can only be used one time.

Instead, just call your task like new MyAsyncTask().execute("");

Threading rules

  • 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..??

  • yes, you can create a callback interface and just declare your method to do the task. Implement the method in other class and then reuse the method with any result you want to send to that method.

Check this Post - common-class-for-asynctask-in-android.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
1

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:

  • perform similar types of tasks which
  • provides relatively similar result(s)

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

CallService.java

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);
    }

}

ServiceCallback.java

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");
SMR
  • 6,628
  • 2
  • 35
  • 56