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