Android Programming Introduction The Open Handset Alliance released the Google Android SDK on November 12th， 2007, having announced it about a week before. The impact was unbelievable, almost every IT/programming-related news- page dumped a news-post about the SDK release – the Google Groups was overwhelmed with over 2000 Messages within the first two Days. The idea of the Android Platform was and still is amazing and is of course attracting more and more programmers every day. Especially the open architecture based on Intents and the possibility to replace even the Home-application grant a really large amount of flexibility to the whole platform. What is Android – a GPhone? The weeks and months before Google released the Android SDK there had been a lot of rumors about a so called GPhone. It was said to be a mobile device manufactured by Google providing free communication by showing context-sensitive advertisements to the user on the device itself. But on November 5th ,2007 Andy Rubin announced: “[The] Android [Platform] – is more significant and ambitious than a single phone.” Google within the Open Handset Alliance (OHA) delivers a complete set of software for mobile devices: an operating system, middleware and key mobile applications. What was released a week later was not a final product,but a “First Look SDK” what many did not realize. Major news sites grabbed the discomforts of some developers who said that Android is full of bugs and heavily lacks of documentation. But the majority says that Android is not buggier than any other software at this stage. Android from above Let’s take a look at what the OHA emphasizes on its Android Platform: Openness “Android was built from the ground-up to enable developers to create compelling mobile applications that take full advantage of all a handset has to offer. It is built to be truly open. For example, an application could call upon any of the phone's core functionality such as making calls, sending text messages, or using the camera, allowing developers to create richer and more cohesive experiences for users.” This is true,as a developer you can do everything, from sending short messages with just 2 lines of code, up to replacing even the HOME- Screen of your device. One could easily create a fully customized operating system within weeks, providing no more of Google’s default application to the user. “Android is built on the open Linux Kernel. Furthermore, it utilizes a custom virtual machine that has been designed to optimize memory and hardware resources in a mobile environment. Android will be open source; it can be liberally extended to incorporate new cutting edge technologies as they emerge. The platform will continue to evolve as the developer community works together to build innovative mobile applications.” Here Google is talking of the so called Dalvik virtual machine (DalvikVM), which is a register based virtual machine, designed and written by Dan Bornstein and some other Google engineers, to be an important part of the Android platform. In the words “register based” we find the first difference to normal Java virtual machines (JVM) which are stack based. All applications are created equal “Android does not differentiate between the phone's core applications and third-party applications. They can all be built to have equal access to a phone's capabilities providing users with a broad spectrum of applications and services. With devices built on the Android Platform, users will be
able to fully tailor the phone to their interests. They can swap out the phone's home screen, the style of the dialer, or any of the applications. They can even instruct their phones to use their favorite photo viewing application to handle the viewing of all photos.” Breaking down application boundaries “Android breaks down the barriers to building new and innovative applications. For example, a developer can combine information from the web with data on an individual's mobile phone - such as the user's contacts, calendar, or geographic location - to provide a more relevant user experience. With Android, a developer could build an application that enables users to view the location of their friends and be alerted when they are in the vicinity giving them a chance to connect.” Fast & easy application development “Android provides access to a wide range of useful libraries and tools that can be used to build rich applications. For example, Android Additionally this application could be enriched with the current GPS- position of the device within a handful of code-lines. Google emphasizes Androids power of providing location-based-services. Google Maps are so neat within Android as if it was just developed for Android. One can integrate a fully zoom and drag enabled map by adding just 3(!) characters in the Java-Code of the Android-Default-Application and 3 lines of XML-Code. Other nice features that are easy to use with Android are Animations and media-playback. Since version m5, the Android SDK contains functions for straight and reverse GeoCoding and in addition to mp3, playback of: ogg-Vorbis, MIDI and a bunch of other formats. What Android tries to do is meet you halfway: You get a commonly used programming language (Java) with some commonly used libraries (e.g., some Apache Commons APIs), with support for tools you may be used to using (Eclipse). You get a fairly rigid and separate framework in which your programs need to run so they can be “good citizens” on the phone and not nterfere with other programs or the operation of the phone itself. As you might expect, much of this book deals with that framework and how to write programs that work within its confines and take advantage of its capabilities. What Androids Are Made Of When you write a desktop application, you are “master of your own domain.” You launch your main window and any child windows—like dialog boxes—that are needed. From your standpoint, you are your own world, leveraging features supported by the operating system, but largely ignorant of any other program that may be running on the computer at the same time. If you do interact with other programs, it is typically through an API, such as Java Database Connectivity (JDBC), or frameworks atop it, to communicate with MySQL or another database. Android has similar concepts, but packaged differently and structured to make phones more crash-resistant. Here are the main components used in an Android application: Activities: The building block of the user interface is the activity. You can think of an activity as being the Android analogue for the window or dialog box in a desktop application. While it is possible for activities to not have a user interface, most likely your “headless” code will be packaged in the form of content providers or services. Content providers: Content providers provide a level of abstraction for any data stored on the device that is accessible by multiple applications. The Android development model encourages you to make your own data available to other applications, as well as your own. Building a content provider lets you do that, while maintaining complete control over how your data is accessed.
Services: Activities and content providers are short-lived and can be shut down at any time. services, on the other hand, are designed to keep running, if needed, independent of any activity. You might use a service for checking for updates to an RSS feed or to play back music even if the controlling activity is no longer operating. Intents: Intents are system messages, running around the inside of the device, notifying applications of various events, from hardware state changes (e.g., an SD card was inserted), to incoming data (e.g., an SMS message arrived), to application events (e.g., your activity was launched from the device’s main menu). Not only can you respond to intents, but you can create your own to launch other activities or to let you know when specific situations arise (e.g., raise such-and-so intent when the user gets within 100 meters of this-and-such location). Stuff at Your Disposal Android comes with a number of features to help you develop applications: Storage: You can package data files with your application, for things that do not change, such as icons or help files. You also can carve out a bit of space on the device itself, for databases or files containing user-entered or retrieved data needed by your application. And, if the user supplies bulk storage, like an SD card, you can read and write files there as needed. Network: Android devices will generally be Internet-ready, through one communications medium or another. You can take advantage of the Internet access at any level you wish, from raw Java sockets all the way up to a built-in WebKit-based web browser widget you can embed in your application. Multimedia: Android devices have the ability to play back and record audio and video. While the specifics may vary from device to device, you can query the device to learn its capabilities, and then take advantage of the multimedia capabilities as you see fit—whether that is to play back music, take pictures with the camera, or use the microphone for audio note-taking. Global positioning system (GPS): Android devices will frequently have access to location providers, such as a GPS, which can tell your applications where the device is on the face of the Earth. In turn, you can display maps or otherwise take advantage of the location data, such as tracking a device’s movements if the device has been stolen. Phone services: Of course, since Android devices are typically phones, your software can initiate calls, send and receive Short Message Service (SMS) messages, and everything else you expect from a modern bit of telephony technology.