问题描述
我已经声明了FileUploadTask的实例,它在onCreate()方法中扩展了AsyncTask
I have declared instance of FileUploadTask which extends AsyncTask in onCreate() method
FileUploadTask uploadTask= null;
并按照以下代码执行后台方法
and executes the background method by following code
public class UploadFiles implements OnClickListener{
....
if(SOTCNetStat.chkConnectionStatus(UploadResult.this)){
uploadTask=new FileUploadTask();
uploadTask.execute("");
}
else
{
Toast.makeText(UploadResult.this, getResources().getString(R.string.Text_CheckNetworkConnections) , Toast.LENGTH_LONG).show();
}
....
}
有取消按钮取消后台进程
Having a cancel button to cancel the background process
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(TAG,"cancel button clicked.....");
//FileUploadTask uploadTask= new FileUploadTask();
if(uploadTask!=null)
{
Log.d(TAG,"click event null checking....");
uploadTask.cancel(true);
}
}
}
在FileUploadTask类中声明了一个boolean来检查运行状态
In FileUploadTask class declared a boolean to check the running status
public class FileUploadTask extends AsyncTask<String, Integer, String> {
....
boolean isRunning=true;
doInBackground方法
The doInBackground method
@Override
protected String doInBackground(String... arg0) {
for(int i=0; i<fp.size(); i++){
index = i+1;
if(isCancelled() && !isRunning)
{
Log.d(TAG,"Cancel 1 Condition Checked ["+i+"]");
Log.d(TAG,"doInBackground canceled");
break;
}
else
{
Log.d(TAG,"Cancel 1 Canceled ["+i+"]");
}
file1 = new File(fp.get(i));
String urlString = Constants.UPLOAD_URL;
try {
Log.e("doInBackground", "urlString: " + urlString);
Log.e("doInBackground", "domainPref: " + domainName);
urlString = urlString.replace("domain", URLEncoder.encode(domainName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody(file1);
ProgressMultipart reqEntity = new ProgressMultipart(new ProgressListener() {
@Override
public void transferred(long num) {
//publishProgress((int) ((num / (float) file1.length() ) * 100));
publishProgress((int) ((num / (float) totalSize ) * 100));
}
});
reqEntity.addPart("userfile", bin1);
if(getIntent().hasExtra("show_id"))
{
//String showId = getIntent().getStringExtra("show_id");
reqEntity.addPart("mobileshow", new StringBody("1"));
reqEntity.addPart("show_ids", new StringBody(getIntent().getStringExtra("show_id")));
}
reqEntity.addPart("Filename", new StringBody(file1.getName()));
reqEntity.addPart("user_id", new StringBody("2"));
reqEntity.addPart("privateasset", new StringBody("true"));
reqEntity.addPart("uploadtype", new StringBody("Normal"));
reqEntity.addPart("version_num", new StringBody("1"));
totalSize = reqEntity.getContentLength();
post.setEntity(reqEntity);
System.err.println("post :"+post.toString());
//to be check the cancel operation
if(isCancelled() && !isRunning)
{
Log.d(TAG,"Cancel 2 Condition Checked ["+i+"]");
Log.d(TAG,"File Uploading Cancelled in doInBackground method");
break;
}
else
{
Log.d(TAG,"Cancel 2 Canceled ["+i+"]");
}
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);
}
....
return response_str;
}
并且重载onCancelled方法
and overloaded onCancelled methods
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
Log.d(TAG,"onCancelled() method called");
super.onCancelled();
}
@Override
protected void onCancelled(String result) {
// TODO Auto-generated method stub
Log.d(TAG,"onCancelled(String) method called");
isRunning=false;
this.cancel(true);
}
我尝试了很多并进行了探索。即使使用取消方法,我也无法在后台停止上传过程。请任何人给出问题的解决方案
我提到的一些链接
I tried a lot and explored . Even using cancel method I can't able to stop the uploading process in background. Please any one give solutions to the problemSome links i referred
推荐答案
您似乎错过了 AsyncTask
的工作方式。仅在 doInBackground()
完成后才会调用 onCancelled()
方法,类似于 onPostExecute()
方法。在调用 uploadTask.cancel(true)
后,不会立即调用它。你使用它的方式,你不需要 onCancelled()
方法或 isRunning
变量(目前在你的代码 isRunning
永远不会更改为 false
因此您的 isCancelled()
检查永远不会起作用)。删除 onCancelled()
方法和 isRunning
变量以及 AsyncTask
将有效。
You seem to be missing how AsyncTask
works. The onCancelled()
method will only be called after doInBackground()
is finished, similar to the onPostExecute()
method. It is not called immediately after uploadTask.cancel(true)
is called as you think it will be. The way you are using it, you have no need for onCancelled()
methods or an isRunning
variable (currently in your code isRunning
is never changed to false
and thus your isCancelled()
check never works). Remove both the onCancelled()
methods and the isRunning
variable and your AsyncTask
will work.
这篇关于即使使用cancel方法也无法取消AsyncTask中的doInBackground进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!