January 17, 2021
Hot Topics:

Handling Lengthy Operations with Android App Widgets | Page 2, Page 2

  • By Shane Conder
  • Send Email »
  • More Articles »

Downloading the Images within the Android Service

The 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 UpdateThread. 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 RemoteViews object.

Sidebar: Cautions

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.


In this article, you've learned how to create a helper Service to handle background processing for an App Widget. In addition, this same Service is used to download the images the App Widgets will display in its slideshow.


Creating a Home Screen App Widget on Android
Handling User Interaction with Android App Widgets

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 androidwirelessdev+a5@gmail.com and via their blog at http://androidbook .blogspot.com.

Page 2 of 2

This article was originally published on October 30, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date