Interrupting/cancelling threads
When you run a background operation using a Thread, or AsyncTask in most of the times it is needed to be able to interrupt it.
In lot of cases, when the user starts a sceen in our application, a thread is started in the background, to load its content. Hoever if the user leaves the screen before its loading is complete, the loading process should be interrupted. Even if the loading prosess can not be interrupted immediately, or it would be too mutch troube, it shold detect that the display of the result is no longer needed.
So how do you stop a thread exactly? You can see in the documentation that Thread class has a stop() method. This method offered an "easy" way to interrupt threads in older android versions, by killing the thread. It is deprecated now, do not use it, it may left things in inconstent state, among other problems, so this was an unelegant way.
The methods you can use is Thread.interrupt() and AsyncTask.cancel(), hoewer just calling them wont kill the process, it will be likely just flagged as interrupted, and continue running. You must implement the interrupt yourself!
In most cases the slow background process has a "main loop", so if you check if the process is interrupted in the loop condition. For example a file download is like that. You can check the Treads interrupted flag with its isInterrupted() method:
- public void run() {
- ...
- while(!isInterrupted() && hasMoreDataToDownload()){
- downloadAndWriteSomeMore();
- }
- if(isInterrupted()){
- deleteThePartiallyDownloadedFi
le(); - }else{
- callBack();
- }
- }
- }
- thread.start();
- thread.interrupt();
If the process is interrupted, you exit the main loop close files, other sesources properly, do some cleanup if nesessary, for example delete the not needed unfinished file. The callBack() function runs othervise. You may want to send a message here to a Handler to indicate somewhere on the user interface that the process ended sucessfully.
If you dont have such main loop in the thread, for example a slow SQL query is executed, and you can not interrupt the query, you still want to care about isInterrupted() after the query run. If the user alredy left the screen that needed the query result, the threads interrupt should be called, so the thread knows a callBack is not needed, the user interface to hold the result may not even exist by that time.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 6 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 6 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 6 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 6 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 6 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN