解决如何让AsyncTask终止操作
受到这个的启发终于结局了如何在AsyncTask运行中终止其操作。
单纯的onCancelled(true)是不行的
下面把代码贴出来~实现了登陆功能。
AsyncTask简介,它使创建需要与用户界面交互的长时间运行的任务变得更简单。相对来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和Handler即可实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | package com.isummation.exampleapp; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URLEncoder; import java.net.UnknownHostException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.json.JSONObject; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class UserLogin extends Activity { private EditText etUsername; private EditText etPassword; private ProgressDialog progressDialog; private static final int PROGRESSDIALOG_ID = 0 ; private static final int SERVER_ERROR = 1 ; private static final int NETWORK_ERROR = 2 ; private static final int CANCELLED = 3 ; private static final int SUCCESS = 4 ; private String ServerResponse; private LoginTask loginTask; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.login); etUsername = (EditText) findViewById(R.id.txt_username); etPassword = (EditText) findViewById(R.id.txt_password); Button login_button = (Button) this .findViewById(R.id.login_button); login_button.setOnClickListener( new OnClickListener() { public void onClick(View viewParam) { if (etUsername.getText().toString().length() == 0 || etPassword.getText().toString().length() == 0 ) { Toast.makeText(getApplicationContext(), "Please enter username and password" , Toast.LENGTH_SHORT).show(); } else { //Show dialog by passing id showDialog(PROGRESSDIALOG_ID); } } }); } protected Dialog onCreateDialog( int id) { switch (id) { case PROGRESSDIALOG_ID: removeDialog(PROGRESSDIALOG_ID); //Please note that forth parameter is true for cancelable Dialog //Also register cancel event listener //if the litener is registered then forth parameter has no effect progressDialog = ProgressDialog.show(UserLogin. this , "Authenticating" , "Please wait..." , true , true , new OnCancelListener(){ public void onCancel(DialogInterface dialog) { //Check the status, status can be RUNNING, FINISHED and PENDING //It can be only cancelled if it is not in FINISHED state if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) loginTask.cancel( true ); } }); break ; default : progressDialog = null ; } return progressDialog; } @Override protected void onPrepareDialog( int id, Dialog dialog) { switch (id) { case PROGRESSDIALOG_ID: //check if any previous task is running, if so then cancel it //it can be cancelled if it is not in FINISHED state if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) loginTask.cancel( true ); loginTask = new LoginTask(); //every time create new object, as AsynTask will only be executed one time. loginTask.execute(); } } class LoginTask extends AsyncTask<Void, Integer, Void> { @Override protected Void doInBackground(Void... unused) { try { ServerResponse = null ; //don't forget to make it null, as task can be called again HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet( getString(R.string.WebServiceURL) + "/cfc/iphonewebservice.cfc?returnformat=json&method=validateUserLogin&username=" + URLEncoder.encode(etUsername.getText() .toString(), "UTF-8" ) + "&password=" + URLEncoder.encode(etPassword.getText() .toString(), "UTF-8" )); httpClient.getParams().setParameter( CoreProtocolPNames.USER_AGENT, "Some user agent string" ); //call it just before you make server call //calling after this statement and canceling task will no meaning if you do some update database kind of operation //so be wise to choose correct place to put this condition //you can also put this condition in for loop, if you are doing iterative task //now this very important //if you do not put this condition and not maintaining execution, then there is no meaning of calling .cancel() method //you should only check this condition in doInBackground() method, otherwise there is no logical meaning if (isCancelled()) { publishProgress(CANCELLED); //Notify your activity that you had canceled the task return ( null ); // don't forget to terminate this method } HttpResponse response = httpClient.execute(httpGet, localContext); BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent(), "UTF-8" )); ServerResponse = reader.readLine(); publishProgress(SUCCESS); //if everything is Okay then publish this message, you may also use onPostExecute() method } catch (UnknownHostException e) { removeDialog(PROGRESSDIALOG_ID); e.printStackTrace(); publishProgress(NETWORK_ERROR); } catch (Exception e) { removeDialog(PROGRESSDIALOG_ID); e.printStackTrace(); publishProgress(SERVER_ERROR); } return ( null ); } @Override protected void onProgressUpdate(Integer... errorCode) { switch (errorCode[ 0 ]) { case CANCELLED: removeDialog(PROGRESSDIALOG_ID); Toast.makeText(getApplicationContext(), "Cancelled by user" , Toast.LENGTH_LONG).show(); break ; case NETWORK_ERROR: removeDialog(PROGRESSDIALOG_ID); Toast.makeText(getApplicationContext(), "Network connection error" , Toast.LENGTH_LONG).show(); break ; case SERVER_ERROR: removeDialog(PROGRESSDIALOG_ID); Toast.makeText(getApplicationContext(), "Server error" , Toast.LENGTH_LONG).show(); break ; case SUCCESS: removeDialog(PROGRESSDIALOG_ID); try { if (ServerResponse != null ) { JSONObject JResponse = new JSONObject(ServerResponse); String sMessage = JResponse.getString( "MESSAGE" ); int success = JResponse.getInt( "SUCCESS" ); if (success == 1 ) { //proceed further //you may start new activity from here //after that you may want to finish this activity UserLogin. this .finish(); //Remember when you finish an activity, it doesn't mean that you also finish thread or AsynTask started within that activity //So you must implement onDestroy() method and terminate those threads. } else { //just showing invalid username password from server response Toast.makeText(getApplicationContext(), sMessage, Toast.LENGTH_SHORT).show(); } } } catch (Exception e){ Toast.makeText(getApplicationContext(), "Server error" , Toast.LENGTH_LONG).show(); e.printStackTrace(); } break ; } } @Override protected void onPostExecute(Void unused) { } } @Override protected void onDestroy(){ //you may call the cancel() method but if it is not handled in doInBackground() method if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) loginTask.cancel( true ); super .onDestroy(); } } |
原文出处:http://www.ericyue.info/archive/stop-asynctask#more-1117
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)