如何在android中停止异步任务线程?

时间:2021-06-17 07:11:40

I want to stop a AsyncTask thread from another AsyncTask thread. I have tried like new AsyncTask.cancel(true) to stop the background process but it didn't stop.

我想从另一个AsyncTask线程停止一个AsyncTask线程。我尝试了new AsyncTask.cancel(true)来停止后台进程,但是它并没有停止。

Could any one help me on this?

有人能帮我一下吗?

6 个解决方案

#1


91  

declare your asyncTask in your activity:

在活动中声明你的asyncTask:

private YourAsyncTask mTask;

instantiate it like this:

实例化它是这样的:

mTask = new YourAsyncTask().execute();

kill/cancel it like this:

这样杀/取消:

mTask.cancel(true);

#2


21  

The reason why things aren't stopping for you is because the process (doInBackground()) runs until it is finished. Therefore you should check if the thread is cancelled or not before doing stuff:

对您来说事情没有停止的原因是进程(doInBackground())一直运行到结束。因此,你应该在做事情之前检查线程是否被取消:

if(!isCancelled()){
// Do your stuff
}

So basically, if the thread is not cancelled, do it, otherwise skip it :) Could be useful to check for this some times during your operation, especially before time taking stuff.

因此,基本上,如果线程没有被取消,那么就执行它,否则跳过:)在操作期间检查一下可能会很有用,尤其是在获取内容之前。

Also it could be useful to "clean up" alittle in

它也可能是有用的“清理”了一点点

onCancelled();

Documentation for AsyncTask:

AsyncTask的文档:

http://developer.android.com/reference/android/os/AsyncTask.html

http://developer.android.com/reference/android/os/AsyncTask.html

Hope this helps!

希望这可以帮助!

#3


10  

You may also have to use it in onPause or onDestroy of Activity Life Cycle:

您可能还必须在活动生命周期的onPause或onDestroy中使用它:

//you may call the cancel() method but if it is not handled in doInBackground() method
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
    loginTask.cancel(true);

where loginTask is object of your AsyncTask

loginTask是AsyncTask的对象吗

Thank you.

谢谢你!

#4


6  

You can't just kill asynctask immediately. In order it to stop you should first cancel it:

您不能立即终止asynctask。为了阻止它,你应该先取消它:

task.cancel(true);

and than in asynctask's doInBackground() method check if it's already cancelled:

而在asynctask的doInBackground()方法中检查它是否已经被取消:

isCancelled()

and if it is, stop executing it manually.

如果是,停止手动执行。

#5


0  

I had a similar problem - essentially I was getting a NPE in an async task after the user had destroyed the fragment. After researching the problem on Stack Overflow, I adopted the following solution:

我遇到了类似的问题——本质上,当用户破坏了片段之后,我在异步任务中获得了一个NPE。在研究了栈溢出问题后,我采用了以下解决方案:

volatile boolean running;

public void onActivityCreated (Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    running=true;
    ...
    }


public void onDestroy() {
    super.onDestroy();

    running=false;
    ...
}

Then, I check "if running" periodically in my async code. I have stress tested this and I am now unable to "break" my activity. This works perfectly and has the advantage of being simpler than some of the solutions I have seen on SO.

然后,我在异步代码中定期检查“是否运行”。我已经做了压力测试,现在我无法“中断”我的活动。它工作得很好,而且比我在上面看到的一些解决方案更简单。

#6


-4  

u can check onCancelled() once then :

你可以检查oncancel()一次:

protected Object doInBackground(Object... x) {

保护对象doInBackground(对象……x){

while (/* condition */) {
  if (isCancelled()) break;
}
return null;

}

}

#1


91  

declare your asyncTask in your activity:

在活动中声明你的asyncTask:

private YourAsyncTask mTask;

instantiate it like this:

实例化它是这样的:

mTask = new YourAsyncTask().execute();

kill/cancel it like this:

这样杀/取消:

mTask.cancel(true);

#2


21  

The reason why things aren't stopping for you is because the process (doInBackground()) runs until it is finished. Therefore you should check if the thread is cancelled or not before doing stuff:

对您来说事情没有停止的原因是进程(doInBackground())一直运行到结束。因此,你应该在做事情之前检查线程是否被取消:

if(!isCancelled()){
// Do your stuff
}

So basically, if the thread is not cancelled, do it, otherwise skip it :) Could be useful to check for this some times during your operation, especially before time taking stuff.

因此,基本上,如果线程没有被取消,那么就执行它,否则跳过:)在操作期间检查一下可能会很有用,尤其是在获取内容之前。

Also it could be useful to "clean up" alittle in

它也可能是有用的“清理”了一点点

onCancelled();

Documentation for AsyncTask:

AsyncTask的文档:

http://developer.android.com/reference/android/os/AsyncTask.html

http://developer.android.com/reference/android/os/AsyncTask.html

Hope this helps!

希望这可以帮助!

#3


10  

You may also have to use it in onPause or onDestroy of Activity Life Cycle:

您可能还必须在活动生命周期的onPause或onDestroy中使用它:

//you may call the cancel() method but if it is not handled in doInBackground() method
if (loginTask != null && loginTask.getStatus() != AsyncTask.Status.FINISHED)
    loginTask.cancel(true);

where loginTask is object of your AsyncTask

loginTask是AsyncTask的对象吗

Thank you.

谢谢你!

#4


6  

You can't just kill asynctask immediately. In order it to stop you should first cancel it:

您不能立即终止asynctask。为了阻止它,你应该先取消它:

task.cancel(true);

and than in asynctask's doInBackground() method check if it's already cancelled:

而在asynctask的doInBackground()方法中检查它是否已经被取消:

isCancelled()

and if it is, stop executing it manually.

如果是,停止手动执行。

#5


0  

I had a similar problem - essentially I was getting a NPE in an async task after the user had destroyed the fragment. After researching the problem on Stack Overflow, I adopted the following solution:

我遇到了类似的问题——本质上,当用户破坏了片段之后,我在异步任务中获得了一个NPE。在研究了栈溢出问题后,我采用了以下解决方案:

volatile boolean running;

public void onActivityCreated (Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    running=true;
    ...
    }


public void onDestroy() {
    super.onDestroy();

    running=false;
    ...
}

Then, I check "if running" periodically in my async code. I have stress tested this and I am now unable to "break" my activity. This works perfectly and has the advantage of being simpler than some of the solutions I have seen on SO.

然后,我在异步代码中定期检查“是否运行”。我已经做了压力测试,现在我无法“中断”我的活动。它工作得很好,而且比我在上面看到的一些解决方案更简单。

#6


-4  

u can check onCancelled() once then :

你可以检查oncancel()一次:

protected Object doInBackground(Object... x) {

保护对象doInBackground(对象……x){

while (/* condition */) {
  if (isCancelled()) break;
}
return null;

}

}