Professional Android 4 Application Development (2012)
Introduction
This is an exciting time for Android developers. Mobile phones have never been more popular; powerful smartphones are now a regular choice for consumers; and the Android ecosystem has expanded to include tablet and TV devices to further expand the audience of your Android applications.
Hundreds of stylish and versatile devices—packing hardware features such as GPS, accelerometers, NFC, and touch screens, combined with reasonably priced data plans—provide an enticing platform upon which to create innovative applications for all Android devices.
Android offers an open alternative for mobile application development. Without artificial barriers, Android developers are free to write applications that take full advantage of increasingly powerful mobile hardware and distribute them in an open market. As a result, developer interest in Android devices has exploded as handset sales have continued to grow. As of 2012, there are hundreds of handset and tablet OEMs, including HTC, Motorola, LG, Samsung, ASUS, and Sony Ericsson. More than 300 million Android devices have been activated, and that number is growing at a rate of over 850,000 activations every day.
Using Google Play for distribution, developers can take advantage of an open marketplace, with no review process, for distributing free and paid applications to all compatible Android devices. Built on an open-source framework, and featuring powerful SDK libraries, Android has enabled more than 450,000 applications to be launched in Google Play.
This book is a hands-on guide to building mobile applications using version 4 of the Android SDK. Chapter by chapter, it takes you through a series of sample projects, each introducing new features and techniques to get the most out of Android. It covers all the basic functionality to get started, as well as the information for experienced mobile developers to leverage the unique features of Android to enhance existing products or create innovative new ones.
Google's philosophy is to release early and iterate often. Since Android's first full release in December 2008, there have been 19 platform and SDK releases. With such a rapid release cycle, there are likely to be regular changes and improvements to the software and development libraries. While the Android engineering team works hard to ensure backward compatibility, future releases are likely to date some of the information provided in this book. Similarly, not all active Android devices will be running the latest platform release.
Wherever possible, I have included details on which platform releases support the functionality described, and which alternatives may exist to provide support for users of older devices. Further, the explanations and examples included will give you the grounding and knowledge needed to write compelling mobile applications using the current SDK, along with the flexibility to quickly adapt to future enhancements.
Who This Book Is For
This book is for anyone interested in creating applications for the Android platform. It includes information that will be valuable, whether you're an experienced mobile developer or making your first foray, via Android, into writing mobile applications.
It will help if you have used smartphones (particularly phones running Android), but it's not necessary, nor is prior experience in mobile application development.
It's expected that you'll have some experience in software development and be familiar with basic object-oriented development practices. An understanding of Java syntax is a requirement, and detailed knowledge and experience is a distinct advantage, though not a strict necessity.
Chapters 1 and 2 introduce mobile development and contain instructions to get you started in Android. Beyond that, there's no requirement to read the chapters in order, although a good understanding of the core components described in Chapters 3 is important before you venture into the remaining chapters. Chapters 10 and 11 cover important details on how to create an application that provides a rich and consistent user experience, while Chapters 12 cover a variety of optional and advanced functionality and can be read in whatever order interest or need dictates.
What This Book Covers
Chapter 1 introduces Android, including what it is and how it fits into existing mobile development. What Android offers as a development platform and why it's an exciting opportunity for creating mobile phone applications are then examined in greater detail.
Chapter 2 covers some best practices for mobile development and explains how to download the Android SDK and start developing applications. It also introduces the Android Developer Tools and demonstrates how to create new applications from scratch.
Chapters 3 take an in-depth look at the fundamental Android application components. Starting with examining the pieces that make up an Android application and its lifecycle, you'll quickly move on to the application manifest and external resources before learning about “Activities,” their lifetimes, and their lifecycles.
You'll then learn how to create basic user interfaces with layouts, Views, and Fragments, before being introduced to the Intent and Broadcast Receiver mechanisms used to perform actions and send messages between application components. Internet resources are then covered, followed by a detailed look at data storage, retrieval, and sharing. You'll start with the preference-saving mechanism and then move on to file handling, databases, and Cursors. You'll also learn how share application data using Content Providers and access data from the native Content Providers. This section finishes with an examination of how to work in the background using Services and background Threads.
Chapters 10 and 11 build on the UI lessons you learned in Chapter 4, examining how to enhance the user experience through the use of the Action Bar, Menu System, and Notifications. You'll learn how to make your applications display-agnostic (optimized for a variety of screen sizes and resolutions), how to make your applications accessible, and how to use speech recognition within your applications.
Chapters 12 look at more advanced topics. You'll learn how to use the compass, accelerometers, and other hardware sensors to let your application react to its environment, and then look at maps and location-based services. Next, you'll learn how your applications can interact with users directly from the home screen using dynamic Widgets, Live Wallpaper, and the Quick Search Box.
After looking at playing and recording multimedia, and using the camera, you'll be introduced to Android's communication capabilities. Bluetooth, NFC, Wi-Fi Direct, and network management (both Wi-Fi and mobile data connections) are covered, followed by the telephony APIs and the APIs used to send and receive SMS messages.
Chapter 18 discusses several advanced development topics, including security, IPC, Cloud to Device Messaging, the License Verification Library, and Strict Mode.
Finally, Chapter 19 examines the options and opportunities available for publishing, distributing, and monetizing your applications—primarily within Google Play.
How This Book Is Structured
This book is structured in a logical sequence to help readers of different development backgrounds learn how to write advanced Android applications. There's no requirement to read each chapter sequentially, but several of the sample projects are developed over the course of several chapters, adding new functionality and other enhancements at each stage.
Experienced mobile developers with a working Android development environment can skim the first two chapters—which are an introduction to mobile development and instructions for creating your development environment—and then dive in at Chapters 3. These chapters cover the fundamentals of Android development, so it's important to have a solid understanding of the concepts they describe.
With this covered, you can move on to the remaining chapters, which look at maps, location-based services, background applications, and more advanced topics, such as hardware interaction and networking.
What You Need to Use This Book
To use the code samples in this book, you will need to create an Android development environment by downloading the Android SDK, developer tools, and the Java Development Kit. You may also want to download and install Eclipse and the Android Developer Tools plug-in to ease your development, but neither is a requirement.
Android development is supported in Windows, Mac OS, and Linux, with the SDK available from the Android web site.
You do not need an Android device to use this book or develop Android applications, though it can be useful—particularly when testing.
Chapter 2 outlines these requirements in more detail and describes where to download and how to install each component.
Conventions
To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.
The pencil icon indicates notes, tips, hints, tricks, and asides to the current discussion.
Boxes with a warning icon like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.
As for styles in the text:
· We show file names, URLs, and code within the text like so: persistence.properties.
· To help readability, class names in text are often represented using a regular font but capitalized like so: Content Provider.
· We present code in two different ways:
· We use a monofont type with no highlighting for most code examples.
· We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.
· In some code samples, you'll see lines marked as follows:
[ ... previous code goes here ... ]
or
[ ... implement something here ... ]
· These represent instructions to replace the entire line (including the square brackets) with actual code, either from a previous code snippet (in the former case) or with your own implementation (in the latter).
· To keep the code samples reasonably concise, I have not always included every import statement required in the code samples. The downloadable code samples described below include all the required import statements. Additionally, if you are developing using Eclipse, you can use the keyboard shortcut Ctrl+Shift+O (Cmd+Shift+O) to insert the required import statements automatically.