I have a AsyncTask as below:
private class SearchTask extends AsyncTask<Object, Object, Object> {
@Override
protected void onPreExecute() {
super.onPreExecute();
System.out.println("Start");
}
@Override
protected Object doInBackground(Object... urls) {
SearchFunction();
return null;
}
@Override
protected void onPostExecute(Object result) {
System.out.println("End");
}
}
And a Timer as below:
private Handler handler = new Handler();
private Runnable updateTimer = new Runnable() {
public void run() {
System.out.println("===status===");
System.out.println(SearchTask.getStatus());
SearchTask.cancel(true);
handler.postDelayed(updateTimer, 3000);
}
};
And call AsyncTask and timer code as below:
handler.postDelayed(updateTimer, 3000);
SearchTask SearchTask = new SearchTask();
SearchTask.execute();
The SearchFunction method in doInBackground maybe spend more than 3 seconds, so add a timer.
But in updateTimer first call and cancel the AsyncTask, the doInBackground is still running until it finished, and then cancel onPostExecute.
How can I do to cancel doInBackground direct?