Table Of ContentAndroid Cookbook
SECOND EDITION
Ian Darwin
Android Cookbook
by Ian F. Darwin Copyright © 2016 O’Reilly Media, Inc.. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc. , 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional
use. Online editions are also available for most titles (
http://safaribooksonline.com ). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
[email protected] .
Editors: Dawn Schanafelt and Meghan Blanchette
Production Editor: FILL IN PRODUCTION EDITOR
Copyeditor: FILL IN COPYEDITOR
Proofreader: FILL IN PROOFREADER
Indexer: FILL IN INDEXER
Interior Designer: David Futato
Cover Designer: Karen Montgomery
Illustrator: Rebecca Demarest
January -4712: Second Edition
Revision History for the Second Edition
2016-10-11: First Early Release
See http://oreilly.com/catalog/errata.csp?isbn=9781449374457 for release
details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Android
Cookbook, the cover image, and related trade dress are trademarks of O’Reilly
Media, Inc.
While the publisher and the author(s) have used good faith efforts to ensure that
the information and instructions contained in this work are accurate, the
publisher and the author(s) disclaim all responsibility for errors or omissions,
including without limitation responsibility for damages resulting from the use of
or reliance on this work. Use of the information and instructions contained in
this work is at your own risk. If any code samples or other technology this work
contains or describes is subject to open source licenses or the intellectual
property rights of others, it is your responsibility to ensure that your use thereof
complies with such licenses and/or rights.
978-1-44937445-7
[FILL IN]
Dedication
To Dennis M. Ritchie (1941–2011), language pioneer and co-inventor of Unix,
who showed us all where the braces go, reminded us to keep it simple, and gave
us so much more…
Preface
===
Ian Darwin
Android is “the open source revolution” applied to cellular telephony and mobile
computing. At least, part of the revolution. There have been many other attempts
to provide open source cell phones, most of them largely defunct, ranging from
the Openmoko FreeRunner to QT Embedded, Moblin, LiMo, Debian Mobile,
Maemo, FireFox OS, Ubuntu Mobile to the open sourced Symbian OS and the
now-defunct HP WebOS. Let’s not forget the established closed source stalwart
Apple’s iOS, and the two minor players (by market share), Microsoft Windows
Mobile and the recently-abandoned BlackBerry OS 10 (both of these have
developer toolkits, but their OS is not available as open source and often has
other “click-wrap” restrictions).
Amongst all these offerings, two stand out as major players. Android is
definitely here to stay! This book is here to help the Android developer
community share the knowledge that will help make better apps. Those who
contribute knowledge here are helping to make Android development easier for
those who come after.
About Android
Android is a mobile technology platform that provides cell phones, tablets, and
other handheld and mobile devices (even netbooks) with the power and
portability of the Linux operating system and the reliability and portability of a
standard high-level language and API and a vast ecosystem of useful
applications. Android apps are mostly written in the Java language, using tools
such as Eclipse and Android Studio, compiled against the Android API, and
translated into bytecode for an Android-specific VM.
Android is thus related by OS family to other Linux-based cell phone projects.
Android is also related by programming language to BlackBerry’s older Java
ME phones, and to Java and the wider realm of Java Enterprise applications. Not
to mention that all current Blackberry devices can run Android applications, and
to mention that all current Blackberry devices can run Android applications, and
in fact Blackberry’s newest devices only run Android.
It’s now generally believed that Android has almost three-quarters of the world
smartphone market, although it has not nearly displaced Apple’s iPad in the
tablet market. Sales figures change all the time, but it is clear that Android is,
and will remain, one of the dominant players in the mobile space.
Who This Book Is By
This book was written by several dozen Android developers from the Android
community at large. Development occurred in the open, on the website
http://androidcookbook.com/, which I built (using Java of course) to allow
people to contribute, view, review, and comment on the recipes that would make
up this book. A complete list can be found in “Acknowledgments”. I am deeply
grateful to all the contributors, who have helped moved this book from a dream
to the reality that you have in your hands (or on-screen if you are reading the
ebook format). Thank you all!
Who This Book Is For
This book focusses on building Android applications using Java, the native
language of Android applications. It is of course possible to package up a web
application as a mobile app (see [Link to Come]), but you will never get the all-
important 100%-correct user experience with all the current features of Android
that way.
So. Java. We assume you know the basics of the Java language. If not, see
Recipe 1.4. We also assume you know the basics of the Java Standard Edition
API (since this forms the basis of Android’s runtime libraries) as well as the
basics of Android. The terms activity, intent, service, and content provider, while
not necessarily being what you dream about at night, should at least be familiar
to you. But if not, we’ve got you covered: see Recipe 1.2.
This book differs from the Android Samples assocated with the Android SDK in
that it tries to focus more on how a given piece of technology works, rather than
by giving you (as the Samples do) a complete, working, example that has both
been simplified (to use very simple data) but complicated by adding in several
“neat” features that are irrelevant to the problem at hand.
What’s in This Book?
Chapter 1 takes you through the steps of setting up the Android development
environment and building several simple applications of the well-known “Hello,
World” type pioneered by Brian Kernighan.
Chapter 2 covers some of the differences in mobile computing that will hit
developers coming from desktop and enterprise software environments, and talks
about how mobile design (in particular, Android design) differs from those other
environments.
Testing is often an afterthought for some developers, so we discuss this early on,
in Chapter 3. Not so that you’ll skip it, but so that you’ll read and heed. We talk
about unit testing individual components as well as testing out your entire
application in a well-controlled way.
Android provides a variety of mechanisms for communicating within an
application and across applications. In Chapter 4 we discuss intents and
broadcast receivers, services, AsyncTasks, and handlers.
Chapter 5 covers a range of topics related to graphics, including use of the
graphical drawing and compositing facilities in Android as well as using desktop
tools to develop graphical images, textures, icons, and so on that will be
incorporated into your finished application.
Every mobile app needs a GUI, so Chapter 6 covers the main ins and outs of
GUI development for Android. Examples are given both in XML and, in a few
cases, in Java-coded GUI development.
[Link to Come] covers all the pop-up mechanisms—menus, dialogs, and toasts
—and one that doesn’t pop up but is also for interaction outside your
application’s window, Android’s notification mechanism.
Lists of items are very common in mobile applications on all platforms. [Link to
Come] focuses on the “list” components in Android, the ListView and its newer
replacement the RecyclerView.
Android is rich in multimedia capabilities. [Link to Come] shows how to use the
most important of these.
[Link to Come] shows how to save data into files, databases, and so on. And
how to retrieve it later, of course. Another communication mechanism is about
allowing controlled access to data that is usually in an SQL database. This
chapter also shows you how to make application data available to other
applications through something as simple but ubiquitous (in Android) as the
URL, and how to use various cloud-based services to store data.
Android started out as an operating system for mobile telephones. [Link to
Come] shows how to control and react to the telephone device that is in most
mobile devices nowadays.
Mobile devices are, for the most part, always-on and always-connected. This has
a major impact on how people use them and think about them. [Link to Come]
shows the coding for traditional networked applications. This is followed by
[Link to Come], and [Link to Come].
The now-ubiquitous Global Positioning System has also had a major impact on
how mobile applications work. [Link to Come] discusses how to find your
location, how to get map data from Google and OpenStreetMap, and how
applications can be location-aware in ways that are just now being explored.
Description:Jump in and build working Android apps with the help of more than 230 tested recipes. The second edition of this acclaimed cookbook includes recipes for working with user interfaces, multitouch gestures, location awareness, web services, and specific device features such as the phone, camera, and ac