You need to save the instance of the each Task (Asynchronous Task) in a ArrayList or any other structure and add your Task object to it
please check following code :
private ArrayList<Task> array_task = new ArrayList<Task>();
private void callTask() {
Task task = new Task() {
@Override
protected void doInBackground(String... params) {
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
string1,string2 );
array_task.add(task);
}
And after when you want to cancel all task you need a for loop again to cancel all check following code
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancel_all_task:
callCancel_AllAsyncTask();
break;
}
}
private void callCancel_AllAsyncTask()
{
for(int i=0;i<array_task.size();i++)
{
array_task.get(i).cancel(true);
}
}