asynctask取消onpostexecute [英] asynctask cancel onpostexecute
本文介绍了asynctask取消onpostexecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在"doInBackground"中捕获,如果应用程序在"doInBackground"中执行catch方法,则我不希望代码运行onPostExecute.代码如下:
i have a try and catch in "doInBackground", i dont wan the code be run the onPostExecute if the apps execute catch method in "doInBackground". code as below:
public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
}
catch{
//some code, don't do onPostExecute after complete
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
//some code
}
@Override
protected void onPostExecute(Void unused) {
//some code
}
}
推荐答案
将布尔值数据类型从AsyncTask的doInBackground()
返回到onPostExecute()
.并检查其值,您想要成功完成什么,
Return boolean data type from your doInBackground()
to onPostExecute()
of your AsyncTask. And check for its values what you want to do for success,
@Override
protected boolean doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
return true;
}
catch{
//some code, don't do onPostExecute after complete
return false;
}
}
和
@Override
protected void onPostExecute(boolean flag) {
if(flag)
{
//some code
}
}
这篇关于asynctask取消onpostexecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文