我有一个使用AsyncTask在后台加载高达100%的程序,并且具有随时取消的功能,所以当我按下“取消”按钮时我会调用此函数:
protected class CancelButtonListener implements View.OnClickListener {
public void onClick(View v) {
_initTask.cancel(true);
}
}
因此,通过文档,它应该调用AsyncTask类中的OnCancelled()函数,但它不能从日志中找到。我再次读到某个地方,在Android 4.0之后没有使用onCancelled(),而是在onPostExecute()上使用isCancelled(),但是再次从我的日志中得到这些函数都没有被调用onPostExecute()和OnCancelled()。 那么处理AsyncTask的CANCEL代码的最佳方法是什么呢,换句话说,我必须要做哪些必要的函数。
我的MainActivity.java
public class MainActivity extends Activity {
protected TextView _percentField;
protected Button _cancelButton;
protected InitTask _initTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_percentField = (TextView) findViewById(R.id.percent_field);
_cancelButton = (Button) findViewById(R.id.cancel_button);
_cancelButton.setOnClickListener(new CancelButtonListener());
_initTask = new InitTask();
_initTask.execute(this);
}
protected class CancelButtonListener implements View.OnClickListener {
public void onClick(View v) {
_initTask.cancel(true);
}
}
/**
* sub-class of AsyncTask
*/
protected class InitTask extends AsyncTask<Context, Integer, String> {
@Override
protected String doInBackground(Context... params) {
int i = 0;
while (i <= 50) {
try {
Thread.sleep(50);
publishProgress(i);
i++;
}
catch (Exception e) {
Log.i("makemachine", e.getMessage());
}
}
return "COMPLETE!";
}
// -- gets called just before thread begins
@Override
protected void onPreExecute() {
Log.i("makemachine", "onPreExecute()");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Log.i("makemachine", "onProgressUpdate(): " + String.valueOf(values[0]));
_percentField.setText((values[0] * 2) + "%");
_percentField.setTextSize(values[0]);
}
@Override
protected void onCancelled() {
customCancelMethod("Cancelled");
}
@Override
protected void onCancelled(String values) {
customCancelMethod(values);
}
protected void customCancelMethod(String val) {
Log.i("makemachine", "onCancelled()"+val);
_percentField.setText("Cancelled!");
_percentField.setTextColor(0xFFFF0000);
}
@Override
protected void onPostExecute(String result) {
if (isCancelled()) {
customCancelMethod(result);
} else {
Log.i("makemachine", "onPostExecute(): " + result+" "+isCancelled());
_percentField.setText(result);
_percentField.setTextColor(0xFF69adea);
_cancelButton.setVisibility(View.INVISIBLE);
}
}
}
}
按下36%的取消按钮时记录:
06-26 03:46:01.281 14889-14889/com.practise.himanshu.loader I/makemachine: onPreExecute()
06-26 03:46:01.358 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 0
06-26 03:46:01.385 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 1
06-26 03:46:01.436 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 2
06-26 03:46:01.487 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 3
06-26 03:46:01.539 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 4
06-26 03:46:01.589 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 5
06-26 03:46:01.640 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 6
06-26 03:46:01.691 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 7
06-26 03:46:01.741 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 8
06-26 03:46:01.792 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 9
06-26 03:46:01.843 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 10
06-26 03:46:01.894 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 11
06-26 03:46:01.944 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 12
06-26 03:46:01.995 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 13
06-26 03:46:02.045 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 14
06-26 03:46:02.096 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 15
06-26 03:46:02.146 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 16
06-26 03:46:02.197 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 17
06-26 03:46:02.248 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 18
当我没有按下取消按钮并让任务完成时记录:
06-26 03:51:01.983 14889-14889/com.practise.himanshu.loader I/makemachine: onPreExecute()
06-26 03:51:02.073 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 0
06-26 03:51:02.096 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 1
06-26 03:51:02.147 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 2
06-26 03:51:02.199 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 3
06-26 03:51:02.250 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 4
06-26 03:51:02.301 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 5
06-26 03:51:02.351 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 6
06-26 03:51:02.402 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 7
06-26 03:51:02.453 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 8
06-26 03:51:02.504 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 9
06-26 03:51:02.554 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 10
06-26 03:51:02.605 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 11
06-26 03:51:02.656 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 12
06-26 03:51:02.708 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 13
06-26 03:51:02.759 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 14
06-26 03:51:02.811 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 15
06-26 03:51:02.861 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 16
06-26 03:51:02.912 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 17
06-26 03:51:02.964 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 18
06-26 03:51:03.015 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 19
06-26 03:51:03.066 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 20
06-26 03:51:03.116 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 21
06-26 03:51:03.167 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 22
06-26 03:51:03.218 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 23
06-26 03:51:03.268 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 24
06-26 03:51:03.319 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 25
06-26 03:51:03.370 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 26
06-26 03:51:03.420 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 27
06-26 03:51:03.471 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 28
06-26 03:51:03.522 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 29
06-26 03:51:03.573 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 30
06-26 03:51:03.624 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 31
06-26 03:51:03.674 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 32
06-26 03:51:03.725 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 33
06-26 03:51:03.775 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 34
06-26 03:51:03.826 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 35
06-26 03:51:03.877 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 36
06-26 03:51:03.928 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 37
06-26 03:51:03.979 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 38
06-26 03:51:04.030 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 39
06-26 03:51:04.080 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 40
06-26 03:51:04.131 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 41
06-26 03:51:04.182 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 42
06-26 03:51:04.233 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 43
06-26 03:51:04.283 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 44
06-26 03:51:04.334 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 45
06-26 03:51:04.385 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 46
06-26 03:51:04.436 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 47
06-26 03:51:04.486 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 48
06-26 03:51:04.537 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 49
06-26 03:51:04.588 14889-14889/com.practise.himanshu.loader I/makemachine: onProgressUpdate(): 50
06-26 03:51:04.598 14889-14889/com.practise.himanshu.loader I/makemachine: onPostExecute(): COMPLETE! false
编辑: 更改MainActivity.java以使用OnCancelled(返回返回)仍然onCancelled未被调用。