mel*_*tcs 1 android android-asynctask
我尝试并在"doInBackground"中捕获,如果应用程序在"doInBackground"中执行catch方法,我不会在代码中运行onPostExecute.代码如下:
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
}
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用cancel(true)
在doInBackground()
.请尝试以下代码:
protected Void doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
}
catch{
cancel(true);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
官方说明 cancel()
调用此方法将导致在doInBackground(Object [])返回后在UI线程上调用onCancelled(Object).调用此方法可确保永远不会调用onPostExecute(Object).调用此方法后,应定期从doInBackground(Object [])检查isCancelled()返回的值,以尽早完成任务.
查看此链接了解更多详情:
http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
归档时间: |
|
查看次数: |
2486 次 |
最近记录: |