简体   繁体   English

AsyncTask取消和多次运行

[英]AsyncTask cancelation and multiple run

I´m facing a problem running my AsyncTask multiple time after its cancelation. 取消后,我多次运行AsyncTask时遇到问题。 Check this: 检查一下:

Calling AsyncTask: 调用AsyncTask:

Log.d("Pre Cancel: ", uploadDB.isCancelled()+"");
uploadDB.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filenameTxt+".db");

AsyncTask code: 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);

}
}

After first run, Log Pre Cancel before AsyncTask wrotes false and then Log Cancel inside it is true, because its canceled in OnPostExecute(). 第一次运行后,在AsyncTask写入false之前记录Log Pre Cancel,然后在其中写入Log Cancel为true,因为在OnPostExecute()中已将其取消。 When I want to run it for second, first Pre Cancel log is true (AsyncTask seems canceled). 当我想第二次运行它时,第一个“预取消”日志为true(AsyncTask似乎已取消)。 However, I cant start it again even it shouldnt be running. 但是,即使它不应该运行,我也无法再次启动它。 Any idea how to run it or cancel properly? 任何想法如何运行它或正确取消? Remember I need to run it as many times as needed! 请记住,我需要根据需要运行多次! And also forgive me other mistakes. 并且也原谅我其他错误。 Just focus on canceling AsyncTask or idea how to run in many times. 只是专注于取消AsyncTask或构思如何多次运行。 Thanks in advance! 提前致谢!

EDIT: I´ve already checked all possible solutions here and on other places. 编辑:我已经在这里和其他地方检查了所有可能的解决方案。 Nothing helped :( 没有帮助:(

From the docs 来自文档

The task can be executed only once (an exception will be thrown if a second execution is attempted.) 该任务只能执行一次(如果尝试第二次执行,则会引发异常。)

You need to create a new instance of the AsyncTask class to run it again. 您需要创建AsyncTask类的新实例才能再次运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM