我想取消一个Asynctask
in android 但我的实现有问题:
我的代码是:
私有类 SynchroTask 扩展 AsyncTask{ 私有 volatile boolean running = true;
@Override
protected void onPreExecute() {
super.onPreExecute();
//INITIALIZATIONS
}
@Override
protected Void doInBackground(Void... params) {
while (running) {
//TASK
}
return null;
}
@Override
protected void onCancelled(){
super.onCancelled();
running = false;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent intent = AccountAddActivity.getIntent(getActivity());
startActivity(intent);
getActivity().finish();
}
}
}
和 :
mSynchroTask = new SynchroTask();
cancelButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (mSynchroTask != null && mSynchroTask.getStatus() != AsyncTask.Status.FINISHED){
HDCApplication.hdcAppManager.mSynchroManager.removeAllTask();
mSynchroTask.cancel(true);
mSynchroTask = null;
}
}
});