Earlier said how to operate the Asynctask, this article we say how to close asynctask.
Someone asked: Why close the asynctask? Quite simply, Asynctask is performing time-consuming operations in the background (getting the data) when you leave the current page or end the process, that is, jumping to your other page does not require the current page of data. If you do not close, the other pages can not display the data, because the background is still getting the current page of data, can also be understood as blocking.
Now let's run the app from the previous blog:
We click Back to go to the app again and click on the production data
We will find that when you click again, the data is not loaded immediately, but only after a while. Some would say that it is a network problem, but we are simulating the data loading without going through the network.
This is the blockage mentioned above. Asynctask when executing multiple threads, is a single execution, and the other threads are in the buffer. When executing a next only start (one by one)
So how do you close it?
Through the API, we will find a asynctask.cancel (true);
A task can is cancelled at any time by invoking Cancel (Boolean). Invoking this method would cause subsequent calls to iscancelled () to return true. After invoking this method, Oncancelled (object), instead of OnPostExecute (object) is invoked after Doinbackground (OBJ Ect[]) returns. To ensure this a task is cancelled as quickly as possible and you should always check the return value of iscancelled () Perio Dically from Doinbackground (object[]), if possible (inside a loop for instance.)
It means:
- We can call cancel at any time to cancel the load task, and calling this method will call iscancelled indirectly and return true.
- After calling cancel (), after Doinbackground () return we will call oncancelled (object) not calling OnPostExecute (object)
- In order to ensure that the task is canceled faster, you should check the Doinbackground () periodically iscancelled to make a judgment.
Now we write the code in the Mainactivity onpause(exit)
@Override protected void OnPause () { ///If the asynchronous task is not empty and the state is running , cancel the load task if (task! =null && Task.getstatus () = = AsyncTask.Status.RUNNING) { task.cancel (true); } Super . OnPause (); }
Then add the red code to the Asynctask and judge
Public classMyTaskextendsasynctask{PrivateList<user>userlist; PrivateBaseadapter Adapter; Privatemainactivity activity; PrivateString Barnum; PublicMyTask (mainactivity activity) { This. Activity =activity; } @Overrideprotectedobject Doinbackground (Object ... params) {userlist= (list<user>) params[0]; Adapter= (Baseadapter) params[1]; for(intI=1; i<=100;i++){ if (iscancelled ()) {break; } Try{Thread.Sleep (300); } Catch(interruptedexception e) {e.printstacktrace (); } userlist.add (NewUser ("Zhangsan" +i,18+i, "AA" +i+ "AA")); Publishprogress (i); Barnum= i+ "%"; } returnuserlist; } @Overrideprotected voidonprogressupdate (Object ... values) {if (iscancelled ()) {return; } Button btn=(Button) Activity.findviewbyid (R.id.button1); Btn.setvisibility (View.gone); TextView Num=(TextView) Activity.findviewbyid (r.id.barnum); intBar = Integer.parseint (values[0].tostring ()); /*bar = (bar) *25;*/ProgressBar ProgressBar=(ProgressBar) Activity.findviewbyid (R.ID.SEEKBAR1); Progressbar.setprogress (bar); Num.settext (Barnum); //Progressbar.setbackgroundcolor (Activity.getresources (). GetColor (R.color.orange)); /*Progressbar.setbackgroundresource (r.color.orange);*/adapter.notifydatasetchanged (); } }
At this point, when I back and then click on the load, immediately began. Some people will find it doubtful: I am not cancel before I quit? Why do you step through the asynchronous task.
When cancel in OnPause, this asynchronous load is canceled, but the iscanceled value in Asynctask is constant, false by default, which does not close.
Thank you for your attention. I am big! It 's The picture of that got small. ----I was huge! It was the pictures that made me small. ("Sunset Boulevard")
Android--Close Asynctask (asynchronous Task)