Android 如何在doInBackground()方法中定期检查isCancelled()
我想取消asynctask并停止其后台执行,我在这个问题中找到了一个解决方案: 但实际上,我在asynctask中的代码是将一些数据上传/发布到服务器,根据上述问题的答案,我不知道如何在这里使用while或forloop和break语句 我的任务是:Android 如何在doInBackground()方法中定期检查isCancelled(),android,android-asynctask,Android,Android Asynctask,我想取消asynctask并停止其后台执行,我在这个问题中找到了一个解决方案: 但实际上,我在asynctask中的代码是将一些数据上传/发布到服务器,根据上述问题的答案,我不知道如何在这里使用while或forloop和break语句 我的任务是: private class Userdataupload extends AsyncTask<String, Void, String> { @Override protected String doInBackgr
private class Userdataupload extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String[] params) {
// do above Server call here
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.example.com/example.php");
int mode = Activity.MODE_PRIVATE;
SharedPreferences my2 = getSharedPreferences("data", mode);
String u = my2.getString("name", "error");
String v = my2.getString("coin", "error");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("name", u));
nameValuePair.add(new BasicNameValuePair("coin", v));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
//tdt(response.toString());
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute()
{
pf = new ProgressDialog(MainActivity.this);
pf.setMessage("Loading User Data...");
pf.show();
pf.setCanceledOnTouchOutside(false);
pf.setCancelable(true);
}
@Override
protected void onPostExecute(String message) {
//process message
pf.cancel();
}
}
私有类Userdataupload扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串[]参数){
//请在这里打电话
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.example.com/example.php");
int mode=Activity.mode\u PRIVATE;
SharedReferences my2=获取SharedReferences(“数据”,模式);
字符串u=my2.getString(“名称”,“错误”);
String v=my2.getString(“硬币”、“错误”);
List nameValuePair=新的ArrayList(2);
添加(新的BasicNameValuePair(“名称”,u));
添加(新的BasicNameValuePair(“硬币”,v));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePair));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
HttpResponse response=httpClient.execute(httpPost);
//将响应写入日志
//tdt(response.toString());
Log.d(“Http Post响应:,Response.toString());
}捕获(客户端协议例外e){
//日志异常
e、 printStackTrace();
}捕获(IOE异常){
//日志异常
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPreExecute()
{
pf=新建进度对话框(MainActivity.this);
setMessage(“正在加载用户数据…”);
pf.show();
pf.SetCanceledOnTouchOut(假);
pf.setCancelable(真);
}
@凌驾
受保护的void onPostExecute(字符串消息){
//处理消息
pf.cancel();
}
}
我想你需要这个
在doinbackground方法中,使用true值在while循环中运行代码段
如果按下iscancelled()
,则将其设置为false并
打破你的后台进程
这是路
`int isExecxute=true;
While(isExecute){
//run your code
//cancelled break the loop and finish your execution
If (iscancelled()) isExecute=false;
}`
在代码片段中有一个循环是没有意义的?如果您不知道首先将循环放在何处,那么您想要实现什么呢?只是我想在doInBackground()方法中定期检查iscancell(),如果iscancell()返回true,则必须销毁异步任务,并且所有后台执行都应该被终止
,而(!iscancell())
。。。