基本上下cancel 之後 他會做完pre back post 目前的區塊,然後不往下個區塊去做。
但在建構式下 cancel 依然會執行pre 的區塊。
Example code
public class AsynTaskCancelExample{
public enum STOP{
CON,PRE,BACK,POST
}
public void start(){
new ExAsynTaskCencel(STOP.CON).execute();
new ExAsynTaskCencel(STOP.PRE).execute();
new ExAsynTaskCencel(STOP.BACK).execute();
new ExAsynTaskCencel(STOP.POST).execute();
}
private class ExAsynTaskCencel extends AsyncTask<Void,Void,Boolean>{
STOP so;
public ExAsynTaskCencel(STOPs){
so = s;
if(so==STOP.CON)cancel(true);
Log.d("DEYU"+so,"cancel");
}
@Override
protected void onPreExecute(){
super.onPreExecute();
if(so==STOP.PRE)cancel(true);
Log.d("DEYU"+so,"onPreExecute");
}
@Override
protected BooleandoInBackground(Void...params){
if(so==STOP.BACK)cancel(true);
Log.d("DEYU"+so,"doInBackground");
return null;
}
@Override
protected void onPostExecute(Booleanresult){
super.onPostExecute(result);
if(so==STOP.POST)cancel(true);
Log.d("DEYU"+so,"onPostExecute");
}
}
}
output Log :
12-11 11:37:55.736: D/DEYUCON(29846): cancel
12-11 11:37:55.736: D/DEYUCON(29846): onPreExecute
12-11 11:37:55.736: D/DEYUPRE(29846): cancel
12-11 11:37:55.737: D/DEYUPRE(29846): onPreExecute
12-11 11:37:55.737: D/DEYUBACK(29846): cancel
12-11 11:37:55.737: D/DEYUBACK(29846): onPreExecute
12-11 11:37:55.737: D/DEYUBACK(29846): doInBackground
12-11 11:37:55.737: D/DEYUPOST(29846): cancel
12-11 11:37:55.737: D/DEYUPOST(29846): onPreExecute
12-11 11:37:55.737: D/DEYUPOST(29846): doInBackground
12-11 11:37:55.741: D/DEYUPOST(29846): onPostExecute
留言列表