Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当doInBackground调用另一个单独类的方法时,如何取消异步任务_Android_Android Asynctask - Fatal编程技术网

Android 当doInBackground调用另一个单独类的方法时,如何取消异步任务

Android 当doInBackground调用另一个单独类的方法时,如何取消异步任务,android,android-asynctask,Android,Android Asynctask,我正在从异步任务的doInBackground调用另一个类的方法。 现在我需要在调用cancel时停止下载。我不确定在哪里检查isCancelled()的值 类myasync扩展了Asynctask{ 受保护的字符串背景(字符串…sURL){ abc=新abc(); abc.getURLResult(sURL[0]) } } abc类() { getURLResult(字符串URL) { 对于(int i=0;i不是很好,但是您可以通过添加静态变量isDownloading来执行类似操作

我正在从异步任务的doInBackground调用另一个类的方法。 现在我需要在调用cancel时停止下载。我不确定在哪里检查isCancelled()的值

类myasync扩展了Asynctask{ 受保护的字符串背景(字符串…sURL){ abc=新abc(); abc.getURLResult(sURL[0]) } } abc类() { getURLResult(字符串URL) {
对于(int i=0;i不是很好,但是您可以通过添加静态变量
isDownloading
来执行类似操作:

protected String doInBackground(String... sURL) {
abc = new abc();
abc.getURLResult(sURL[0])
}
}

class abc()
{

getURLResult(String URL)
{

 for(int i=0; i<fp.size(); i++){
     if(!myclass.isDownloading){ //ADDED
         break; // or Return or handle Cancel
     }
//some text to dopwnload
}

}

class myclass
{

public static boolean isDownloading; // ADDED

myclass()
{
myasync = new myasync();
isDownloading = true; // ADDED
myasync.execute("http:\\");
}
   stopDownload()
   {
    isDownloading = false; // ADDED
    myclass.cancel(true);
    }
}
protectedstring doInBackground(String…sURL){
abc=新abc();
abc.getURLResult(sURL[0])
}
}
abc类()
{
getURLResult(字符串URL)
{

对于(int i=0;i更新:

从异步任务取消。我们必须检查异步任务是否如您所说被取消

调用此方法将导致调用onCancelled(对象) 在doInBackground(Object[])返回后的UI线程上 方法保证永远不会调用onPostExecute(对象) 调用此方法时,应检查 定期从doInBackground(对象[])执行isCancelled()以完成 尽早完成任务

为此,请将asyncTask本身作为参数与URL一起发送到getURLResult:

protected String doInBackground(String... sURL) {
    new abc().getURLResult("http://...", this); // this here is the asyncTask itself.
}

getURLResult(String URL, myasync myAsyncTask)
{
    for(int i=0; i<fp.size(); i++){
        if(myAsyncTask.isCancelled()){
            break;
        }
    }
}
要实现此目的,请将以下cancel方法添加到AsyncTask

public final boolean cancel(boolean mayInterruptIfRunning) {
    askedForCancellation = true;
    return mFuture.cancel(mayInterruptIfRunning);
}
在你们班:

myasync.cancel(true);
myasync = null;
将myasync设置为null是可以的。因为,您不能再使用它执行。您将得到一个运行时错误。您需要重新初始化它

检查AsyncTask是否要求取消。检查 myasync等于null。还记得要求获取的AsyncTask吗 已取消和未取消,因为无法保证 将在调用cancel时被取消。您所做的是忽略 在执行
onPostExecute


到目前为止,我在超过15个应用程序中使用了这种方法。没有bug,也没有意外行为。

我需要退出for循环。我需要中断下载并停止下载。完成后将调用Onpostexecute()。如果调用cancel(true),将执行onCancelled()而不是Onpostexecute()。虽然这是决定是否向客户端发送响应的一个好方法。你搞错了。你的问题是错误的。你必须说完成下载。或者停止下载。通过调用cancel方法取消异步意味着我不再需要数据和连接。请不要使用cancel。只要中断循环,反正这是测试。只要中断af即可第10次迭代!是的,如果我调用cancel(true),for循环应该中断,数据连接应该断开。一旦for循环中断,DoInBackground将完成并被取消。我需要在某个地方调用IsCancell()来中断循环。不知道在哪里调用它。当我编写方法“public final boolean cancel”时(boolean MayInterruptFrunning)“”在aysnc类中,我遇到错误“无法重写AsyncTask中的最终方法”。我可以调用其他方法“myasyc.cancel1()'它将调用'abc.cancel1',但这将在ui线程中调用。doInBackground在单独的线程中运行。abc类到底是什么。它将存在于实际应用程序中吗?您是否尝试过在单击取消时将myasync对象设置为null。垃圾收集器应负责其余部分。尚未测试。没有immedia调用cancel()时也可以回调,否则可以在该回调中设置静态变量,或者从abc调用某个方法并在其中设置某个abc变量。
private boolean askedForCancellation = true;

@Override
protected void onPostExecute(Object response) {
    if (!askedForCancellation)
       // parse response
    else
       // ignore. or send message to activity to stop loading if you didn't already did that when called cancel method.
}
public final boolean cancel(boolean mayInterruptIfRunning) {
    askedForCancellation = true;
    return mFuture.cancel(mayInterruptIfRunning);
}
myasync.cancel(true);
myasync = null;