How can I cancel Android asyntask?

Source: Internet
Author: User

A very important thing is that asyntask has never been called to cancel the call. Now I have encountered a problem and want to cancel it?

The following are some of my understanding on the Internet, but it is best to experiment on your own before deciding how to do it.

Finally, I decided to process it in the ondesdroy () method of fragment as follows:

 @Override    public void onDestroy() {        super.onDestroy();        if (mGetFileLength != null && !mGetFileLength.isCancelled()) {            mGetFileLength.cancel(true);        }     }

Bytes ----------------------------------------------------------------------------------------------------

Asynctask cannot be switched off using the cancel function.
If the cancel method is called when asynctask is executing sleep, an exception occurs.
If you want to disable asynctask in the middle, you 'd better set a flag and query the flag in doinbackground.

An asynctask object can only be executed once. To execute the object for the second time, you must recreate it.

Bytes -------------------------------------------------------------------------------------------------------

In Java threads, there is no way to stop a running thread. The same applies to asynctask in Android. If you must stop a thread, you can use this thread to set a flag, and then judge the flag in the key steps in the thread run method or the doinbackground method of asynctask to determine whether to continue execution. Then, change the flag where the thread needs to be terminated to stop the thread.

 

   The cancel method that calls asynctask externally cannot stop a started asynctask. The cancel method is similar to the thread's interrupt method. After a thread's interrupt method is called, the thread still runs, however, if the thread's run method is in sleep or wait status after calling sleep or wait, sleep and wait will immediately end and interruptedexception will be thrown. The Cancel Method of asynctask is the same. If the sleep or wait method is called in the doinbackground method of the task, after the Cancel Method of the task instance is called in the UI thread, sleep or wait ends immediately and an interruptedexception exception is thrown. However, if there is other code behind the code that captures the exception, the code will continue to be executed.

Bytes ------------------------------------------------------------------------------------------------------------

Http://hi.baidu.com/justtmiss/item/e67ebf5a964ed4cad2e10cf0

Http://www.cnblogs.com/tt-0411/archive/2012/03/25/2410830.html

Http://blog.csdn.net/cynhafa/article/details/6860201

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.