我在取消后多次运行我的AsyncTask时遇到问题。 检查一下:
调用AsyncTask:
Log.d("Pre Cancel: ", uploadDB.isCancelled()+"");
uploadDB.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filenameTxt+".db");
AsyncTask代码:
package com.example.rtls;
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.content.ContextWrapper;
import android.os.AsyncTask;
import android.util.Log;
public class UploadDB extends AsyncTask<String,Integer,String>{
public FTPClient client = null;
public boolean status=false;
int i=0;
@Override
protected String doInBackground(String... filenameTxt)
{
while(i==0){
client = new FTPClient();
try {
client.connect(Global.IP,Integer.parseInt(Global.port));
client.login(Global.user,Global.pass);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
/* Set File Transfer Mode
*
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
* for transferring text, image, and compressed files.
*/
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
}
Log.d("Filename: ", filenameTxt[0]);
client.changeWorkingDirectory("/RTLS/databases/");
FileInputStream fis = new FileInputStream (new File("/sdcard/rtls/Databases/"+filenameTxt[0]));
status=client.storeFile(filenameTxt[0], fis);
fis.close();
if (this.isCancelled())
{
// Thread.currentThread().interrupt();
Global.progressDialog.dismiss();
break;
}
i=1;
} catch (Exception e) {
e.printStackTrace();
try {
client.logout();
client.disconnect();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Global.progressDialog.show();
}
/**
* update průběhu progressDialog
*/
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
Global.progressDialog.setIndeterminate(false);
Global.progressDialog.setMax(100);
Global.progressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result)
{
Global.progressDialog.dismiss();
this.cancel(true);
Log.d("Cancel: ", this.isCancelled()+"");
}
@Override
protected void onCancelled(String result)
{
Global.progressDialog.dismiss();
//super.cancel(true);
}
}
首次运行后,在AsyncTask wrotes false之前记录预先取消,然后在其中记录取消为真,因为它在OnPostExecute()中被取消。 当我想要运行它第二次时,首先Pre Cancel log为真(AsyncTask似乎被取消)。但是,即使它不应该运行,我也无法再次启动它。 知道如何运行或取消正确吗?记住我需要根据需要多次运行它!并且还原谅我的其他错误。只需专注于取消AsyncTask或想法如何多次运行。 提前谢谢!
编辑: 我已经在这里和其他地方检查了所有可能的解决方案。没有任何帮助:(