Professional Android 4 Application Development

Professional Android 4 Application Development

by Reto Meier




Developers, build mobile Android apps using Android 4

The fast-growing popularity of Android smartphones and tabletscreates a huge opportunities for developers. If you're anexperienced developer, you can start creating robust mobile Androidapps right away with this professional guide to Android 4application development. Written by one of Google's lead Androiddeveloper advocates, this practical book walks you through a seriesof hands-on projects that illustrate the features of the AndroidSDK. That includes all the new APIs introduced in Android 3 and 4,including building for tablets, using the Action Bar, Wi-Fi Direct,NFC Beam, and more.

  • Shows experienced developers how to create mobile applicationsfor Android smartphones and tablets
  • Revised and expanded to cover all the Android SDK releasesincluding Android 4.0 (Ice Cream Sandwich), including all updatedAPIs, and the latest changes to the Android platform.
  • Explains new and enhanced features such as drag and drop,fragments, the action bar, enhanced multitouch support, newenvironmental sensor support, major improvements to the animationframework, and a range of new communications techniques includingNFC and Wi-Fi direct.
  • Provides practical guidance on publishing and marketing yourapplications, best practices for user experience, and more

This book helps you learn to master the design, lifecycle, andUI of an Android app through practical exercises, which you canthen use as a basis for developing your own Android apps.

Product Details

ISBN-13: 9781118102275
Publisher: Wiley
Publication date: 05/01/2012
Pages: 864
Product dimensions: 7.40(w) x 9.20(h) x 1.70(d)

About the Author

Reto Meier is Google's Android Developer Relations Tech Lead. He has been involved in Android development since the initial release in 2007.

Read an Excerpt

Professional Android 4 Application Development

By Reto Meier

John Wiley & Sons

Copyright © 2012 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-1-1181-0227-5

Chapter One

Hello, Android


* A background of mobile application development

* What Android is (and what it isn't)

* An introduction to the Android SDK features

* Which devices Android runs on

* Why you should develop for mobile and Android

* An introduction to the Android SDK and 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 an increasingly wide range of 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 applications that can shape the look, feel, and function of the devices on which they run.

Small, stylish, and versatile, modern mobile devices have become powerful tools that incorporate touchscreens, cameras, media players, Global Positioning System (GPS) receivers, and Near Field Communications (NFC) hardware. As technology has evolved, mobile phones have become about much more than simply making calls. With the introduction of tablets and Google TV, Android has expanded beyond its roots as a mobile phone operating system, providing a consistent platform for application development across an increasingly wide range of 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 access, video recording, location-based services, support for background services, map-based activities, relational databases, inter-application communication, Bluetooth, NFC, and 2D and 3D graphics.

This book describes how to use these APIs to create your own Android applications. In this chapter you'll learn some guidelines for mobile and embedded hardware development, as well as be introduced to some of the platform features available for Android development.

Android has powerful APIs, excellent documentation, a thriving developer community, and no development or distribution costs. As mobile devices continue to increase in popularity, and Android devices expand into exciting new form-factors, you have the opportunity to create innovative applications no matter what your development experience.


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 ubiquitous and indispensable.

Hardware advancements have made mobiles smaller and more efficient while featuring bigger, brighter screens and including an increasing number of hardware peripherals.

After first including cameras and media players, mobiles now feature GPS receivers, accelerometers, NFC hardware, and high-definition touchscreens. These hardware innovations offer fertile ground for software development, but until relatively recently the applications available for mobile phones have lagged behind their hardware counterparts.

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, typically 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.

Platforms such as Symbian were later created to provide developers with a wider target audience. These systems proved more successful in encouraging mobile developers to provide rich applications that better leveraged the hardware available.

Although these platforms did, and continue to, offer some access to the device hardware, they generally required developers 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, such as 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 (JVM), a process that abstracts the underlying hardware and lets developers create applications that run on the wide variety of devices that support the Java run time. Unfortunately, this convenience comes at the price of restricted access to the device hardware.

In mobile development, it was long 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 were regular desktop programs or websites designed to render on a smaller screen, and didn't take advantage of the inherent mobility of the handheld platform.

Living in the Future

Android sits alongside a new wave of modern mobile operating systems designed to support application development on increasingly powerful mobile hardware. Platforms like Microsoft's Windows Phone and the Apple iPhone also provide a richer, simplified development environment for mobile applications; however, unlike Android, they're built on proprietary operating systems. In some cases they 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 applications 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's alternative; indeed, even the dialer and home screens can be replaced.


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 the following:

* A Java ME implementation — Android applications are written using the Java language, but they are not run within a Java ME (Mobile Edition) VM, 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 (such as UIQ or S60) — Although 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), whereas Android is an open-source software stack produced and supported by the Open Handset Alliance (OHA) and designed to operate on any compatible device.


Google's Andy Rubin describes Android as follows:

The first truly open and comprehensive platform for mobile devices. It includes an operating system, user-interface and applications — all of the software to run a mobile phone but without the proprietary obstacles that have hindered mobile innovation.

—Where's My Gphone? ( wheres-my-gphone.html)

More recently, Android has expanded beyond a pure mobile phone platform to provide a development platform for an increasingly wide range of hardware, including tablets and televisions.

Put simply, Android is an ecosystem made up of a combination of three components:

* A free, open-source operating system for embedded devices

* An open-source development platform for creating 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 Compatibility Definition Document (CDD) and Compatibility Test Suite (CTS) that describe the capabilities required for a device to support the software stack.

* A Linux operating system kernel that provides a low-level interface with the hardware, memory management, and process control, all optimized for mobile and embedded 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 (VM) 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, databases, telephony, and sensors.

* A user interface framework used to host and launch applications.

* A set of core pre-installed applications.

* A software development kit (SDK) used to create applications, including the related 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.


Android devices typically come with a suite of preinstalled applications that form part of the Android Open Source Project (AOSP), including, but not necessarily limited to, the following:

* 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

* A home screen

* An alarm clock

In many cases Android devices also ship with the following proprietary Google mobile applications:

* The Google Play Store for downloading third-party Android applications

* A fully featured mobile Google Maps application, including StreetView, driving directions, and turn-by-turn navigation, satellite views, and traffic conditions

* The Gmail email client

* The Google Talk instant-messaging client

* The YouTube video player

The data stored and used by many of these native applications — such as contact details — are also available to third-party applications. Similarly, your applications can respond to events such as incoming calls.

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 Sense, Motorola with MotoBlur, and Samsung with TouchWiz.

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.


The true appeal of Android as a development environment lies in its APIs.

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:

* GSM, EDGE, 3G, 4G, and LTE 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 and network-based location detection

* Full support for applications that integrate map controls as part of their user interfaces

* Wi-Fi hardware access and peer-to-peer connections

* Full multimedia hardware control, including playback and recording with the camera and microphone

* Media libraries for playing and recording a variety of audio/video or still-image formats

* APIs for using sensor hardware, including accelerometers, compasses, and barometers

* Libraries for using Bluetooth and NFC hardware for peer-to-peer data transfer

* IPC message passing

* Shared data stores and APIs for contacts, social networking, calendar, and multi-media

* Background Services, applications, and processes

* Home-screen Widgets and Live Wallpaper

* The ability to integrate application search results into the system searches

* An integrated open-source HTML5 WebKit-based browser

* Mobile-optimized, hardware-accelerated graphics, including a path-based 2D graphics library and support for 3D graphics using OpenGL ES 2.0

* Localization through a dynamic resource framework

* An application framework that encourages the reuse of application components and the replacement of native applications

Access to Hardware, Including Camera, GPS, and Sensors

Android includes API libraries to simplify development involving the underlying device hardware. They 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, sensors (including accelerometers), NFC, the touchscreen, and power management. You can explore the possibilities of some of Android's hardware APIs in more detail in Chapters 12 and 15–17.

Data Transfers Using Wi-Fi, Bluetooth, and NFC

Android offers rich support for transferring data between devices, including Bluetooth, Wi-Fi Direct, and Android Beam. These technologies offer a rich variety of techniques for sharing data between paired devices, depending on the hardware available on the underlying device, allowing you to create innovative collaborative applications.

In addition, Android offers APIs to manage your network connections, Bluetooth connections, and NFC tag reading.

Details on using Android's communications APIs are available in Chapter 16, "Bluetooth, NFC, Networks, and Wi-Fi."

Maps, Geocoding, and Location-Based Services

Embedded map support enables you to create a range of map-based applications that leverage the mobility of Android devices. Android lets you design user interfaces that include interactive Google Maps that you can control programmatically and annotate using Android's rich graphics library.

Android's location-based services manage technologies such as GPS and Google's network-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 selecting a particular technology. This also means your location-based applications will work no matter what technology the host device 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 13, "Maps, Geocoding, and Location-Based Services."

Background Services

Android supports applications and services designed to run in the background while your application isn't being actively used.

Modern mobiles and tablets are by nature multifunction devices; however, their screen sizes and interaction models mean that generally only one interactive application is 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 event-driven 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.

Notifications are the standard means by which a mobile device traditionally alerts users to events that have happened in a background application. Using the Notification Manager, you can trigger audible alerts, cause vibration, and flash the device's LED, as well as control status bar notification icons.

Learn more about how to use Notifications and get the most out of background services in Chapters 9 and 10.

SQLite Database for Data Storage and Retrieval

Rapid and efficient data storage and retrieval are essential for a device whose storage capacity is relatively limited.


Excerpted from Professional Android 4 Application Development by Reto Meier Copyright © 2012 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.

Table of Contents


CHAPTER 1 Hello, Android 1

CHAPTER 2 Getting Started 19

CHAPTER 3 Creating Applications and Activities 53

CHAPTER 4 Building User Interfaces 95

CHAPTER 5 Intents and Broadcast Receivers 165

CHAPTER 6 Using Internet Resources 201

CHAPTER 7 Files, Saving State, and Preferences 221

CHAPTER 8 Databases and Content Providers 251

CHAPTER 9 Working in the Background 331

CHAPTER 10 Expanding the User Experience 359

CHAPTER 11 Advanced User Experience 425

CHAPTER 12 Hardware Sensors 481

CHAPTER 13 Maps, Geocoding, and Location-Based Services 513

CHAPTER 14 Invading the Home Screen 565

CHAPTER 15 Audio, Video, and Using the Camera 621

CHAPTER 16 Bluetooth, NFC, Networks, and Wi-Fi 665

CHAPTER 17 Telephony and SMS 701

CHAPTER 18 Advanced Android Development 739

CHAPTER 19 Monetizing, Promoting, and Distributing Applications 771


Customer Reviews