受到这个的启发终于结局了如何在AsyncTask运行中终止其操作。
单纯的onCancelled(true)是不行的
下面把代码贴出来~实现了登陆功能。
AsyncTask简介,它使创建需要与用户界面交互的长时间运行的任务变得更简单。相对来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和Handler即可实现。
转自stackoverflow
001 | package com.isummation.exampleapp; |
003 | import java.io.BufferedReader; |
004 | import java.io.InputStreamReader; |
005 | import java.net.URLEncoder; |
006 | import java.net.UnknownHostException; |
008 | import org.apache.http.HttpResponse; |
009 | import org.apache.http.client.HttpClient; |
010 | import org.apache.http.client.methods.HttpGet; |
011 | import org.apache.http.impl.client.DefaultHttpClient; |
012 | import org.apache.http.params.CoreProtocolPNames; |
013 | import org.apache.http.protocol.BasicHttpContext; |
014 | import org.apache.http.protocol.HttpContext; |
015 | import org.json.JSONObject; |
017 | import android.app.Activity; |
018 | import android.app.Dialog; |
019 | import android.app.ProgressDialog; |
020 | import android.content.DialogInterface; |
021 | import android.content.DialogInterface.OnCancelListener; |
022 | import android.os.AsyncTask; |
023 | import android.os.Bundle; |
024 | import android.view.View; |
025 | import android.view.View.OnClickListener; |
026 | import android.widget.Button; |
027 | import android.widget.EditText; |
028 | import android.widget.Toast; |
030 | public class UserLogin extends Activity { |
032 |
private EditText etUsername; |
033 |
private EditText etPassword; |
034 |
private ProgressDialog progressDialog; |
035 |
private static final int PROGRESSDIALOG_ID = 0 ; |
036 |
private static final int SERVER_ERROR = 1 ; |
037 |
private static final int NETWORK_ERROR = 2 ; |
038 |
private static final int CANCELLED = 3 ; |
039 |
private static final int SUCCESS = 4 ; |
040 |
private String ServerResponse; |
041 |
private LoginTask loginTask; |
044 |
public void onCreate(Bundle savedInstanceState) { |
045 |
super .onCreate(savedInstanceState); |
046 |
setContentView(R.layout.login); |
048 |
etUsername = (EditText) findViewById(R.id.txt_username); |
049 |
etPassword = (EditText) findViewById(R.id.txt_password); |
051 |
Button login_button = (Button) this .findViewById(R.id.login_button); |
052 |
login_button.setOnClickListener( new OnClickListener() { |
053 |
public void onClick(View viewParam) { |
054 |
if (etUsername.getText().toString().length() == 0 |
055 |
|| etPassword.getText().toString().length() == 0 ) { |
056 |
Toast.makeText(getApplicationContext(), |
057 |
"Please enter username and password" , |
058 |
Toast.LENGTH_SHORT).show(); |
062 |
showDialog(PROGRESSDIALOG_ID); |
068 |
protected Dialog onCreateDialog( int id) { |
070 |
case PROGRESSDIALOG_ID: |
071 |
removeDialog(PROGRESSDIALOG_ID); |
076 |
progressDialog = ProgressDialog.show(UserLogin. this , "Authenticating" , |
077 |
"Please wait..." , true , true , new OnCancelListener(){ |
079 |
public void onCancel(DialogInterface dialog) { |
082 |
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) |
083 |
loginTask.cancel( true ); |
088 |
progressDialog = null ; |
090 |
return progressDialog; |
094 |
protected void onPrepareDialog( int id, Dialog dialog) { |
096 |
case PROGRESSDIALOG_ID: |
099 |
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) |
100 |
loginTask.cancel( true ); |
101 |
loginTask = new LoginTask(); |
106 |
class LoginTask extends AsyncTask<Void, Integer, Void> { |
108 |
protected Void doInBackground(Void... unused) { |
110 |
ServerResponse = null ; |
111 |
HttpClient httpClient = new DefaultHttpClient(); |
112 |
HttpContext localContext = new BasicHttpContext(); |
113 |
HttpGet httpGet = new HttpGet( |
114 |
getString(R.string.WebServiceURL) |
115 |
+ "/cfc/iphonewebservice.cfc?returnformat=json&method=validateUserLogin&username=" |
116 |
+ URLEncoder.encode(etUsername.getText() |
119 |
+ URLEncoder.encode(etPassword.getText() |
120 |
.toString(), "UTF-8" )); |
121 |
httpClient.getParams().setParameter( |
122 |
CoreProtocolPNames.USER_AGENT, "Some user agent string" ); |
134 |
publishProgress(CANCELLED); |
137 |
HttpResponse response = httpClient.execute(httpGet, |
140 |
BufferedReader reader = new BufferedReader( |
141 |
new InputStreamReader( |
142 |
response.getEntity().getContent(), "UTF-8" )); |
143 |
ServerResponse = reader.readLine(); |
144 |
publishProgress(SUCCESS); |
145 |
} catch (UnknownHostException e) { |
146 |
removeDialog(PROGRESSDIALOG_ID); |
148 |
publishProgress(NETWORK_ERROR); |
149 |
} catch (Exception e) { |
150 |
removeDialog(PROGRESSDIALOG_ID); |
152 |
publishProgress(SERVER_ERROR); |
158 |
protected void onProgressUpdate(Integer... errorCode) { |
159 |
switch (errorCode[ 0 ]) { |
161 |
removeDialog(PROGRESSDIALOG_ID); |
162 |
Toast.makeText(getApplicationContext(), "Cancelled by user" , |
163 |
Toast.LENGTH_LONG).show(); |
166 |
removeDialog(PROGRESSDIALOG_ID); |
167 |
Toast.makeText(getApplicationContext(), "Network connection error" , |
168 |
Toast.LENGTH_LONG).show(); |
171 |
removeDialog(PROGRESSDIALOG_ID); |
172 |
Toast.makeText(getApplicationContext(), "Server error" , |
173 |
Toast.LENGTH_LONG).show(); |
176 |
removeDialog(PROGRESSDIALOG_ID); |
178 |
if (ServerResponse != null ) { |
179 |
JSONObject JResponse = new JSONObject(ServerResponse); |
180 |
String sMessage = JResponse.getString( "MESSAGE" ); |
181 |
int success = JResponse.getInt( "SUCCESS" ); |
187 |
UserLogin. this .finish(); |
193 |
Toast.makeText(getApplicationContext(), sMessage, |
194 |
Toast.LENGTH_SHORT).show(); |
198 |
Toast.makeText(getApplicationContext(), "Server error" , |
199 |
Toast.LENGTH_LONG).show(); |
207 |
protected void onPostExecute(Void unused) { |
213 |
protected void onDestroy(){ |
215 |
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED) |
216 |
loginTask.cancel( true ); |
原文出处:http://www.ericyue.info/archive/stop-asynctask#more-1117