What's new in Android 2.0? Part 2: Developer features
Android 2.0 ("Eclair") is an evolutionary version of the Android platform. Despite its version number, it's not as big a change as going from 1.1 to 1.5 ("Cupcake"). However, it does bring a number of notable features to the platform. This article samples the goodies that Google sprinkled on top of Eclair just for developers.
After months of speculation, Android 2.0 ("Eclair") was officially released this week by Google. To be more precise, the Software Development Kit (SDK) for Android 2.0 was released, so that developers can get a head start on 2.0 development before the new devices with 2.0 hit the market. It's not much of a head start, though, because the new devices may will be on sale as early as next week, or by the end of November at the latest.
This is the second of a series of 3 articles covering what's new in this new release. Part 1 covered user features and availability, this part will cover developer features, and part 3 will try to answer any other questions you might have about the new platform. You should read part 1 first, if you haven't already.
Over the next few months, we expect to see more and more Android devices being released. These devices will be running Android 1.5, 1.6, or 2.0. We are also planning a minor version update of Android 2.0 towards the end of the year, and that will be the last update for 2009.
That's right, folks - by the end of this year there will be 4 different versions of Android running in the field. It's going to be a challenge for developers to keep on top of this. One thing seems clear - all Android devices will not automatically be upgraded to the latest version:
Android 1.5 provides the base functionality common to all versions of Android.
Android 1.6 adds support for screen sizes smaller and larger than HVGA 480x320.
Android 2.0 adds support for multi-touch and virtual keys, among other things.
Android 2.1 will probably be a minor bug-fix version.
With rare exceptions, 1.5/1.6 apps will run fine on 2.0 phones. Most apps will not need the new features that 2.0 brings, so they can continue to use the 1.5 SDK and get the greatest possible reach. Other apps can test to see if they're running on 2.0 and only use new features if they are available. A few apps, however, will want to require 2.0 and rely on its features even if it means they can only be run on a few new devices (for now, anyhow).
The first thing you'll notice about the new Android 2.0 SDK is... that there is no Android 2.0 SDK. It's called "SDK Tools, Revision 3". Why? Because of all those versions mentioned before. The idea is that you'll download the "SDK Tools" once, and then install "packages" for different Android Platforms you want to support. I recommend you install the 1.5, 1.6, and 2.0 modules as a bare minimum. Also, be sure to install the Google API module if you're going to use Google Maps. Heck, just install everything. Twice.
And don't try to update from your existing 1.6 SDK. I don't care what Google says - start fresh. You'll need a new version of the Android Eclipse plug-in too. That one you can upgrade.
Nine hundred ninety-nine bottles of pop on the wall...
There are a ton of new developer-oriented features and APIs in Android 2.0. "A ton" equals 999, if you just go by the API Differences Report. The following are the most important ones I feel you should know about:
MotionEvent can now report simultaneous-touch information for devices that support it. Up to three pointers can be tracked simultaneously. Why three?
KeyEvent has new key dispatching APIs, to help implement action-on-up and long press behavior, as well a new mechanism to cancel key presses (for virtual keys).
Google Gears is deprecated, and its functionality has been folded into the WebView class. For example, there are new APIs to get your GeoLocation or store data in a web database.
Bluetooth enhancements including peer-to-peer discovery (the android.bluetooth package).
Centralized account manager API, securely stores and accesses authentication tokens or passwords (the android.accounts package). The API can support asynchronous long-running network-based authentication methods.
New APIs for sync adapters to connect to any backend. You could use this to synchronize data with a company database or third party mail server. Authentication is provided by the centralized account manager.
New contacts APIs and reusable contact badges (for apps that interact with the user's contact list).
New Service life-cycle APIs.
New APIs for retrieving memory usage statistics on other applications.
New APIs for setting the home screen's wallpaper.
New constants that allow a window to wake up the screen when it is displayed and show the window even if the screen is locked. This allows applications to more cleanly implement things like alarm clocks that should wake the device.
New Intent APIs that broadcast the docking state of the device and allow applications to launch special activities when the device is placed in a desktop or car dock.
The KeyEvent changes might break older applications that rely on performing an action on the key-down event for the HOME, MENU, BACK, and SEARCH keys. Those will no longer fire on Android 2.0. The solution is to use a new method like onBackPressed(), or to capture both the key-down and key-up events and use a flag to track the state of the event, performing the action on key-up only if key-down was pressed and focus hasn't changed. Or even better, don't try to capture these buttons yourself - let the Android system do it.
The foreground background service
Another possibly breaking change in Android 2.0 is the deprecation of the Service.setForeground() method. Actually it wasn't just deprecated, it was turned into a no-op. According to the documentation,
This method has been turned into a no-op rather than simply being deprecated because analysis of numerous poorly behaving devices has shown that increasingly often the trouble is being caused in part by applications that are abusing it. Thus, given a choice between introducing problems in existing applications using this API (by allowing them to be killed when they would like to avoid it), vs allowing the performance of the entire system to be decreased, this method was deemed less important.
By default services are run in the background, meaning that if the system needs to kill them to reclaim more memory, they can be killed without too much harm. But if killing your service would be disruptive to the user, such as if your service is performing background music playback - the user would notice if their music stopped playing - you can tell Android it's a foreground service using the new Service.startForeground() method.
Hit the snooze button
As an example of what you can do with the new 2.0 API, look at the new Motorola Droid phone. When you insert it sideways into its cradle, an app uses the docking broadcast event to bring up an alarm clock application. Eventually the screen turns off and the key lock is enabled, but when your alarm goes off it can turn on the screen and display the app's alarm clock screen. And voila, you have yourself a very expensive alarm clock. You can imagine something similar happening when you plug your phone into a car dock like the one TomTom makes for the iPhone. It could bring up your favorite GPS tracking application without your having to search for it.
Android 2.0 ("Eclair") is an evolutionary version of the Android platform. Despite its version number, it's not as big a change as going from 1.1 to 1.5 ("Cupcake"). However, it does bring a number of notable features to the platform. Existing applications, especially those that were already adapted to run with multiple screen sizes on Android 1.6 ("Donut"), will run just fine on version 2.0 devices. New applications can take advantage of new features such as multi-touch and BlueTooth discovery if the hardware supports it.
Unfortunately there will not be a quick and automatic transition to 2.0 like there was with version 1.5. For one thing, device manufacturers are just now coming out with 1.6 devices, and some are even running 1.5. The great HTC Hero phone, for example, is still running 1.5 (albeit with a few patches for things like multi-touch).
While 2.0 is all new and shiny, keep in mind that no devices run it right now and only 2 are expected to run it by the end of the year. Some devices may never see any version beyond 1.5 or 1.6. I predict the tipping point where 2.0 devices outnumber 1.5/1.6 devices won't occur until the middle of 2010. Just keep that in mind when you're deciding which version to target with your next killer app.