Handling Lengthy Operations with Android App Widgets | Page 2, Page 2
Downloading the Images within the Android Service
UpdateThread, shown briefly in the previous section, handles the downloading of the images from the feed. All this thread does is wait around for a specific amount of time to pass before downloading an XML file, parsing it for images, then downloading those images--also on a separate worker thread. The details around the XML parsing and image downloading are up to the developer; any method will work fine. The result, however, is important. The images must be stored locally and a reference should be kept in a
Vector attached to the
Vector objects are synchronized for us, so we can use this to pass the image references easily back to the main Service. This reference is then used during an App Widget update to configure the
Although this article has illustrated how to allow for App Widgets requiring lengthy background operations using an Android Service, there are numerous housekeeping items to be aware of for production code that have not been addressed in our example. For example, when writing networking code, care should be taken to avoid excessive network data calls which could result in surprising bills to end-users. Busy background services should also do their best to avoid draining the battery of the handset. Additionally, the App Widget service should manage its resources prudently (e.g. by limiting its local storage requirements). For example, each image file could be resized before it's stored to reduce the disk space required by the App Widget. Along the same lines, the image files themselves should be persistently indexed to survive across handset resets. Many methods are available for handling these issues; however, the details of how are up to the developer and the specific App Widget implementation.
About the Authors
Shane Conder is a software developer focused on mobile and web technologies. He is currently working at a small mobile software company. With almost two decades of experience in software production, Lauren Darcey specializes in the development of commercial grade mobile applications. Recently, Shane and Lauren coauthored an in-depth programming book entitled Android Wireless Application Development, available from Addison-Wesley (ISBN: 0321627091). They are now working on an entry-level Android book, coming in Spring 2010. They can be reached at firstname.lastname@example.org and via their blog at http://androidbook .blogspot.com.
Page 2 of 2