RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Android Mobility: Open Source Hits the Road : Page 5

Learn to leverage Android's powerful APIs to rapidly create sophisticated applications for media, data storage, and networking.

Android's features include always-on networking; much like the iPhone, you can think of it as an Internet terminal in your pocket. So let's make this a 21st-century application by allowing you to easily share your pictures with the world.

The uploader UI, shown in Listing 8, is simple. Another new View appears here: ProgressBar. This View indicates that the application is working.

To make this look better, apply the "Dialog" theme when adding the new Activity to AndroidManifest.xml. This will make the upload appear as a modal dialog above the previous screen:

<activity android:name=".Uploader" android:label="@string/app_name"
How will you handle the actual upload? Unlike the other examples so far, you should not use onCreate, onPause, or any of the other Android life cycle points. These methods are invoked by the program's solitary UI thread; if too much time is taken during the network upload, nothing else on the phone will be able to draw or react. The solution is clear to Java developers: create a new thread to handle the network.

Android ships with the standard java.io and java.net packages. It also includes the org.apache.http package for HTTP networking. This example uses a fictional simple socket connection for uploading, but you can easily adapt it to match whatever your server expects (see Listing 9).

When reviewing the code for the Uploader, Java developers may be struck by how ordinary the uploading code appears. By avoiding specialized classes, Android offers a gentle learning curve to experienced Java SE and Java EE developers.

One concept requires some special explanation: the Handler. As mentioned above, Android uses only a single UI thread. If your new Thread attempts to update an element on the screen while the main UI thread is also trying to update, this may corrupt the state. The solution is to define a Handler, which is a simple class that receives messages. When your thread wants to update something on the screen, it creates a Message and sends it to the Handler. The Handler will be invoked in the main UI thread where it can safely update the screen—in this situation, stopping the progress animation and displaying some text.

Where Do We Go From Here?
By writing four Activities, four XML files, and one helper class, you have created an application that exercises many Android capabilities:

  • You used a variety of Android's Views and arranged them into different layouts.
  • You have created a SQLite database that provides access to image records.
  • You have learned how to capture, store, view, and send camera images.
  • You can navigate between Activities.
  • You can update information in a database.
  • You can communicate over the network to an external server.
You should now have the tools to go out and create your own Android applications. If you'd like to enhance ConnectedCamera, here are some things you could explore:
  • Instead of showing the names of photos in the Album, show thumbnail images.
  • Add the ability to delete images. Don't forget to clean up the filesystem as well as the database!
  • Try registering your images with the Image ContentProvider so other applications can access them.
  • Use geocoding to record where each image was taken.
Programming in Android is coding on the bleeding edge. It offers solid functionality and design, at the cost of a rapidly evolving platform that doesn't always work as advertised. After handsets start to ship, you may look back at these hectic early days with nostalgia. In the meantime, get out there, code, and have fun!

Chris King is a senior software engineer at Gravity Mobile and is the author of Advanced BlackBerry Development (published by Apress). When he isn't programming or writing for fun or profit, Chris can be found reading, baking, cycling, or hiking throughout the San Francisco Bay Area.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date