Read an Excerpt
Professional Android 2 Application Development
By Reto Meier
John Wiley & SonsCopyright © 2010 John Wiley & Sons, Ltd
All right reserved.
Chapter OneHello, Android
WHAT'S IN THIS CHAPTER?
* A background to mobile application development
* What Android is (and what it isn't)
* An introduction to the Android SDK features
* What devices Android runs on
* Why develop for mobile and Android?
* An introduction to the SDK and the Android development framework
Whether you're an experienced mobile engineer, a desktop or web developer, or a complete programming novice, Android represents an exciting new opportunity to write innovative applications for mobile devices.
Despite the name, Android will not help you create an unstoppable army of emotionless robot warriors on a relentless quest to cleanse the earth of the scourge of humanity. Instead, Android is an open-source software stack that includes the operating system, middleware, and key mobile applications along with a set of API libraries for writing mobile applications that can shape the look, feel, and function of mobile handsets.
Small, stylish, and versatile, modern mobile devices have become powerful tools that incorporate cameras, media players, GPS systems, and touchscreens. As technology has evolved, mobile phones have become about more than simply making calls, but their software and development platforms have struggled to keep pace.
Until recently, mobile phones were largely closed environments built on highly fragmented, proprietary operating systems that required proprietary development tools. The phones themselves often prioritized native applications over those written by third parties. This has introduced an artificial barrier for developers hoping to build on increasingly powerful mobile hardware.
In Android, native and third-party applications are written with the same APIs and executed on the same run time. These APIs feature hardware sensor access, video recording, location-based services, support for background services, map-based activities, relational databases, inter-application communication, and 2D and 3D graphics.
Using this book, you will learn how to use these APIs to create your own Android applications. In this chapter you'll learn some mobile development guidelines and be introduced to the features available from the Android development platform.
Android has powerful APIs, excellent documentation, a thriving developer community, and no development or distribution costs. As mobile devices continue to increase in popularity, this is an exciting opportunity to create innovative mobile phone applications no matter what your development experience.
A LITTLE BACKGROUND
In the days before Twitter and Facebook, when Google was still a twinkle in its founders' eyes and dinosaurs roamed the earth, mobile phones were just that — portable phones small enough to fit inside a briefcase, featuring batteries that could last up to several hours. They did however offer the freedom to make calls without being physically connected to a landline.
Increasingly small, stylish, and powerful mobile phones are now as ubiquitous as they are indispensable. Hardware advancements have made mobiles smaller and more efficient while including an increasing number of peripherals.
After first getting cameras and media players, mobiles now include GPS systems, accelerometers, and touch screens. While these hardware innovations should prove fertile ground for software development, the applications available for mobile phones have generally lagged behind the hardware.
The Not-So-Distant Past
Historically, developers, generally coding in low-level C or C++, have needed to understand the specific hardware they were coding for, generally a single device or possibly a range of devices from a single manufacturer. As hardware technology and mobile Internet access has advanced, this closed approach has become outmoded.
More recently, platforms like Symbian have been created to provide developers with a wider target audience. These systems have proven more successful in encouraging mobile developers to provide rich applications that better leverage the hardware available.
These platforms offer some access to the device hardware, but require the developer to write complex C/C++ code and make heavy use of proprietary APIs that are notoriously difficult to work with. This difficulty is amplified for applications that must work on different hardware implementations and those that make use of a particular hardware feature, like GPS.
In more recent years, the biggest advance in mobile phone development was the introduction of Javahosted MIDlets. MIDlets are executed on a Java virtual machine, a process that abstracts the underlying hardware and lets developers create applications that run on the wide variety of devices that supports the Java run time. Unfortunately, this convenience comes at the price of restricted access to the device hardware.
In mobile development it was considered normal for third-party applications to receive different hardware access and execution rights from those given to native applications written by the phone manufacturers, with MIDlets often receiving few of either.
The introduction of Java MIDlets expanded developers' audiences, but the lack of low-level hardware access and sandboxed execution meant that most mobile applications are regular desktop programs or web sites designed to render on a smaller screen, and do not take advantage of the inherent mobility of the handheld platform.
Android sits alongside a new wave of mobile operating systems designed for increasingly powerful mobile hardware. Windows Mobile, the Apple iPhone, and the Palm Pre now provide a richer, simplified development environment for mobile applications. However, unlike Android, they're built on proprietary operating systems that in some cases prioritize native applications over those created by third parties, restrict communication among applications and native phone data, and restrict or control the distribution of third-party apps to their platforms.
Android offers new possibilities for mobile applications by offering an open development environment built on an open-source Linux kernel. Hardware access is available to all applications through a series of API libraries, and application interaction, while carefully controlled, is fully supported.
In Android, all applications have equal standing. Third-party and native Android applications are written with the same APIs and are executed on the same run time. Users can remove and replace any native application with a third-party developer alternative; even the dialer and home screens can be replaced.
WHAT IT ISN'T
As a disruptive addition to a mature field, it's not hard to see why there has been some confusion about what exactly Android is. Android is not:
* A Java ME implementation Android applications are written in the Java language, but they are not run within a Java ME virtual machine, and Java-compiled classes and executables will not run natively in Android.
* Part of the Linux Phone Standards Forum (LiPS) or the Open Mobile Alliance (OMA) Android runs on an open-source Linux kernel, but, while their goals are similar, Android's complete software stack approach goes further than the focus of these standards-defining organizations.
* Simply an application layer (like UIQ or S60) While Android does include an application layer, "Android" also describes the entire software stack encompassing the underlying operating system, the API libraries, and the applications themselves.
* A mobile phone handset Android includes a reference design for mobile handset manufacturers, but there is no single "Android phone." Instead, Android has been designed to support many alternative hardware devices.
* Google's answer to the iPhone The iPhone is a fully proprietary hardware and software platform released by a single company (Apple), while Android is an open-source software stack produced and supported by the Open Handset Alliance and designed to operate on any handset that meets the requirements. Google has now released its first direct-to-consumer handset, the Nexus 1, but this device remains simply one hardware implementation running on the Android platform.
ANDROID: AN OPEN PLATFORM FOR MOBILE DEVELOPMENT
Google's Andy Rubin describes Android as:
The first truly open and comprehensive platform for mobile devices, all of the software to run a mobile phone but without the proprietary obstacles that have hindered mobile innovation. (http://googleblog.blogspot.com/2007/11/ wheres-my-gphone.html)
Put simply, Android is a combination of three components:
* A free, open-source operating system for mobile devices
* An open-source development platform for creating mobile applications
* Devices, particularly mobile phones, that run the Android operating system and the applications created for it
More specifically, Android is made up of several necessary and dependent parts, including the following:
* A hardware reference design that describes the capabilities required for a mobile device to support the software stack.
* A Linux operating system kernel that provides low-level interface with the hardware, memory management, and process control, all optimized for mobile devices.
* Open-source libraries for application development, including SQLite, WebKit, OpenGL, and a media manager.
* A run time used to execute and host Android applications, including the Dalvik virtual machine and the core libraries that provide Android-specific functionality. The run time is designed to be small and efficient for use on mobile devices.
* An application framework that agnostically exposes system services to the application layer, including the window manager and location manager, content providers, telephony, and sensors.
* A user interface framework used to host and launch applications.
* Preinstalled applications shipped as part of the stack.
* A software development kit used to create applications, including tools, plug-ins, and documentation.
What really makes Android compelling is its open philosophy, which ensures that you can fix any deficiencies in user interface or native application design by writing an extension or replacement. Android provides you, as a developer, with the opportunity to create mobile phone interfaces and applications designed to look, feel, and function exactly as you imagine them.
NATIVE ANDROID APPLICATIONS
Android phones will normally come with a suite of generic preinstalled applications that are part of the Android Open Source Project (AOSP), including, but not necessarily limited to:
* An e-mail client
* An SMS management application
* A full PIM (personal information management) suite including a calendar and contacts list
* A WebKit-based web browser
* A music player and picture gallery
* A camera and video recording application
* A calculator
* The home screen
* An alarm clock
In many cases Android devices will also ship with the following proprietary Google mobile applications:
* The Android Market client for downloading third-party Android applications
* A fully-featured mobile Google Maps application including StreetView, driving directions and turn-by-turn navigation, satellite view, and traffic conditions
* The Gmail mail client
* The Google Talk instant-messaging client
* The YouTube video player
The data stored and used by many of these native applications — like contact details — are also available to third-party applications. Similarly, your applications can handle events such as incoming calls or new SMS messages.
The exact makeup of the applications available on new Android phones is likely to vary based on the hardware manufacturer and/or the phone carrier or distributor.
The open-source nature of Android means that carriers and OEMs can customize the user interface and the applications bundled with each Android device. Several OEMs have done this, including HTC with the Sense UI, Motorola with MotoBlur, and Sony Ericsson's custom UI.
It's important to note that for compatible devices, the underlying platform and SDK remain consistent across OEM and carrier variations. The look and feel of the user interface may vary, but your applications will function in the same way across all compatible Android devices.
ANDROID SDK FEATURES
The true appeal of Android as a development environment lies in the APIs it provides.
As an application-neutral platform, Android gives you the opportunity to create applications that are as much a part of the phone as anything provided out of the box. The following list highlights some of the most noteworthy Android features:
* No licensing, distribution, or development fees or release approval processes
* Wi-Fi hardware access
* GSM, EDGE, and 3G networks for telephony or data transfer, enabling you to make or receive calls or SMS messages, or to send and retrieve data across mobile networks
* Comprehensive APIs for location-based services such as GPS
* Full multimedia hardware control, including playback and recording with the camera and microphone
* APIs for using sensor hardware, including accelerometers and the compass
* Libraries for using Bluetooth for peer-to-peer data transfer
* IPC message passing
* Shared data stores
* Background applications and processes
* Home-screen Widgets, Live Folders, and Live Wallpaper
* The ability to integrate application search results into the system search
* An integrated open-source HTML5 WebKit-based browser
* Full support for applications that integrate map controls as part of their user interface
* Mobile-optimized hardware-accelerated graphics, including a path-based 2D graphics library and support for 3D graphics using OpenGL ES 2.0
* Media libraries for playing and recording a variety of audio/video or still image formats
* Localization through a dynamic resource framework
* An application framework that encourages reuse of application components and the replacement of native applications
Access to Hardware, Including Camera, GPS, and Accelerometer
Android includes API libraries to simplify development involving the device hardware. These ensure that you don't need to create specific implementations of your software for different devices, so you can create Android applications that work as expected on any device that supports the Android software stack.
The Android SDK includes APIs for location-based hardware (such as GPS), the camera, audio, network connections, Wi-Fi, Bluetooth, accelerometers, the touchscreen, and power management. You can explore the possibilities of some of Android's hardware APIs in more detail in Chapters 11 through 14.
Native Google Maps, Geocoding, and Location-Based Services
Native map support lets you create a range of map-based applications that leverage the mobility of Android devices. Android lets you create activities that include interactive Google Maps as part of your user interface, with full access to maps that you can control programmatically and annotate using Android's rich graphics library.
Android's location-based services manage technologies like GPS and Google's GSM cell-based location technology to determine the device's current position. These services enforce an abstraction from specific location-detecting technology and let you specify minimum requirements (e.g., accuracy or cost) rather than choosing a particular technology. They also mean that your location-based applications will work no matter what technology the host handset supports.
To combine maps with locations, Android includes an API for forward and reverse geocoding that lets you find map coordinates for an address, and the address of a map position. You'll learn the details of using maps, the Geocoder, and location-based services in Chapter 8.
Android supports applications and services designed to run invisibly in the background.
Modern mobiles are by nature multifunction devices; however, their limited screen sizes means that generally only one interactive application can be visible at any time. Platforms that don't support background execution limit the viability of applications that don't need your constant attention.
Background services make it possible to create invisible application components that perform automatic processing without direct user action. Background execution allows your applications to become eventdriven and to support regular updates, which is perfect for monitoring game scores or market prices, generating location-based alerts, or prioritizing and prescreening incoming calls and SMS messages.
Learn more about how to get the most out of background services in Chapter 9.
Excerpted from Professional Android 2 Application Development by Reto Meier Copyright © 2010 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.