Android™ Application Development
Cookbook
93 Recipes for Building Winning Apps
Wei-Meng Lee
Android™ Application Development Cookbook: 93 Recipes for Building Winning Apps
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-17767-9
ISBN: 978-1-118-22729-9 (ebk)
ISBN: 978-1-118-24028-1 (ebk)
ISBN: 978-1-118-26491-1 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201)
748-6008, or online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media
such as a CD or DVD that is not included in the version you purchased, you may download this material at http://
booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2012948549
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are
the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned
in this book.
To my family,
Thanks for the understanding and support while I
worked on getting this book ready. I love you all!
About the Author
Wei-Meng Lee is a technologist and founder of Developer Learning Solutions (www.learn2develop
.net), a technology company specializing in hands-on training on the latest mobile technologies.
Wei-Meng has many years of training experience and his courses place special emphasis on the learning-by-doing approach. This hands-on approach to learning programming makes understanding the
subject much easier than reading books, tutorials, and other documentation.
Wei-Meng is also the author of Beginning iOS 5 Application Development (Wrox, 2010)
and Beginning Android 4 Application Development (Wrox, 2011). You can contact him at
About the Technical Editor
Chaim Krause is a Simulation Specialist at the US Army’s Command and General Staff College
where he develops various software products on a multitude of platforms, from iOS and Android
devices to Windows desktops and Linux servers, among other duties. Python is his preferred language,
but he is multilingual and also codes in Java, JavaScript/HTML5/CSS, and others. He was fortunate to
begin his professional career in the software field at Borland where he was a Senior Developer Support
Engineer for Delphi. Outside of computer geek stuff, Chaim enjoys techno and dubstep music, and
scootering with his two sled dogs Dasher and Minnie.
Credits
Executive Editor
Production Manager
Robert Elliott
Tim Tate
Senior Project Editor
Vice President and Executive Group
Publisher
Ami Frank Sullivan
Richard Swadley
Technical Editor
Chaim Krause
Vice President and Executive Publisher
Neil Edde
Production Editor
Christine Mugnolo
Associate Publisher
Jim Minatel
Copy Editor
Luann Rouff
Project Coordinator, Cover
Katie Crocker
Editorial Manager
Mary Beth Wakefield
Compositor
Craig Johnson, Happenstance Type-O-Rama
Freelancer Editorial Manager
Rosemarie Graham
Proofreader
Scott Klemp, Word One New York
Associate Director of Marketing
David Mayhew
Indexer
Robert Swanson
Marketing Manager
Ashley Zurcher
Cover Designer
Ryan Sneed
Business Manager
Amy Knies
Cover Image
© Paul Fleet / iStockPhoto
Acknowledgments
A lot of development in the Android world has happened since my last book, Beginning Android 4
Application Development, went to print. Google has released a new version of the SDK: Android 4.1
SDK. With the Android 4.1 SDK and the ADT Plugin 20.0.3, it is now much easier to write Android
applications — from those that run on older devices right up to the latest and greatest.
I would like to thank some key people who have worked hard behind the scenes to make this book
a reality.
First, my personal gratitude to Bob Elliott, executive editor at Wrox. Bob is always ready to lend a
listening ear and offer help when it’s needed. It is a great pleasure to work with Bob, as he is one of
the most responsive people I have ever worked with. Thank you, Bob, for the help and guidance.
Of course, I cannot forget Ami Sullivan, my editor (and friend), who is always a pleasure to work
with. Thank you for your guidance and encouragement to keep the project going, Ami.
I am also grateful to my technical editor, Chaim Krause. Chaim has been eagle-eye editing the book
and testing my recipes, ensuring that my code works as written. Thanks, Chaim.
Last but not least, I want to thank my parents and my wife, Sze Wa, for all the support they have
given me. They have selflessly adjusted their schedules to accommodate my busy schedule when I
was working on this book. My wife, as always, has stayed up with me on numerous nights as I was
furiously working to meet the deadlines, and for this I would like to say to her and my parents, “I
love you all!” Finally, to our lovely dog, Ookii, thanks for staying by our side.
Contents
Introduction
Chapter 1: Android Fundamentals
Recipe 1.1 Linking Activities
xv
1
1
Solution2
Recipe 1.2 Passing Data between Activities
5
Solution6
Recipe 1.3 Passing Objects between Activities
11
Solution11
Recipe 1.4 Sending and Receiving Broadcasts
13
Solution14
Recipe 1.5 Assigning Priorities to Broadcast Receivers
18
Solution19
Recipe 1.6 Auto-Launching Your Application at Boot Time
21
Solution21
Recipe 1.7 Calling Built-In Apps
22
Solution23
Recipe 1.8 Making Your Application Callable by Others
32
Solution32
Chapter 2: Using Views to Design the User Interface
Recipe 2.1 Using Buttons
39
40
Solution40
Recipe 2.2 Using Image Buttons
44
Solution44
Recipe 2.3 Using Radio Buttons
46
Solution46
Recipe 2.4 Using CheckBoxes
49
Solution50
Recipe 2.5 Implementing a Star Rating System
52
Solution52
Recipe 2.6 Using AutoCompleteTextView
55
Solution55
Recipe 2.7 Displaying Web Pages
57
Solution58
CONTENTS
Recipe 2.8 Using the TimePicker
62
Solution62
Recipe 2.9 Using the DatePicker
64
Solution65
Recipe 2.10 Using LinearLayout for View Positioning
66
Solution67
Recipe 2.11 Using RelativeLayout for View Positioning
72
Solution72
Recipe 2.12 Using FrameLayout for View Positioning
74
Solution74
Recipe 2.13 Using TableLayout for View Positioning
77
Solution77
Recipe 2.14 Using the ScrollView
79
Solution79
Recipe 2.15 Displaying Context and Options Menus
83
Solution83
Recipe 2.16 Displaying Dialogs
90
Solution91
Recipe 2.17 Implementing Paging
98
Solution98
Chapter 3: Displaying Lists of Items and Images
Recipe 3.1 Displaying a List of Items Using the ListView
105
106
Solution106
Recipe 3.2 Customizing the ListView
108
Solution108
Recipe 3.3 Displaying Multiple ListViews
111
Solution111
Recipe 3.4 Creating Custom ListViews
114
Solution115
Recipe 3.5 Further Customizing Each Row with Additional
TextViews117
Solution Part One: Adding Additional TextViews
Solution Part Two: Recycling Each Row
Recipe 3.6 Displaying a List of Items Using the Spinner View
118
122
125
Solution126
Recipe 3.7 Displaying a List of Images
128
Solution129
Recipe 3.8 Animating the Changing of Images Using the
ImageSwitcher133
Solution133
x
CONTENTS
Recipe 3.9 Displaying Images Using the GridView
138
Solution138
Recipe 3.10 Building a Master-Detail User Interface
141
Solution143
Chapter 4: Telephony
Recipe 4.1 Calling from Your Application
151
151
Solution152
Recipe 4.2 Monitoring the State of the Phone
153
Solution153
Recipe 4.3 Monitoring Phone State in the Background
156
Solution156
Recipe 4.4 Blocking Outgoing Calls
158
Solution159
Recipe 4.5 Auto-Answering an Incoming Call
160
Solution160
Recipe 4.6 Switching to Airplane Mode
163
Solution163
Recipe 4.7 Getting the Phone Number, IMEI, and SIM Card ID
165
Solution166
Recipe 4.8 Enabling Bluetooth
167
Solution167
Recipe 4.9 Displaying the Call Log
173
Solution173
Chapter 5: Messaging
Recipe 5.1 Sending SMS Messages Through the Built-in
Messaging Application
175
175
Solution176
Recipe 5.2 Sending SMS Messages Programmatically in Your
Android Application
178
Solution178
Recipe 5.3 Monitoring the Status of Sent SMS Messages
Programmatically179
Solution180
Recipe 5.4 Monitoring Outgoing SMS Messages
183
Solution184
Recipe 5.5 Intercepting Incoming SMS Messages
187
Solution187
xi
CONTENTS
Chapter 6: Network Programming
Recipe 6.1 Connecting to Servers Using HTTP GET
191
191
Solution192
Recipe 6.2 Connecting to Servers Using HTTP POST
195
Solution195
Recipe 6.3 Downloading Binary Data Using HTTP
198
Solution199
Recipe 6.4 Consuming XML Web Services
200
Solution201
Recipe 6.5 Consuming JSON Web Services
204
Solution205
Recipe 6.6 Getting the IP Address of the Device
210
Solution211
Recipe 6.7 Creating a Socket Server
212
Solution212
Recipe 6.8 Creating a Socket Client
218
Solution218
Recipe 6.9 Checking for the Availability of Bluetooth
222
Solution222
Recipe 6.10 Monitoring the State of Bluetooth
224
Solution225
Recipe 6.11 Creating a Bluetooth Chat Application
226
Solution227
Chapter 7: Using Google Maps
Recipe 7.1 Displaying Google Maps
243
243
Solution244
Recipe 7.2 Zooming in and out of Google Maps
250
Solution250
Recipe 7.3 Changing Map Modes
253
Solution253
Recipe 7.4 Navigating the Map to a Particular Location
256
Solution256
Recipe 7.5 Adding Markers to the Map
258
Solution258
Recipe 7.6 Finding a User-Friendly Address Using Reverse
Geocoding, and Vice Versa
268
Solution268
Recipe 7.7 Responding to Zooming and Panning
275
Solution275
xii
CONTENTS
Chapter 8: Location-Based Data Services
Recipe 8.1 Obtaining Geographical Location Using GPS, Wi-Fi, or
Cellular Networks
279
280
Solution280
Recipe 8.2 Choosing the Best Location Provider to Use
284
Solution284
Recipe 8.3 Monitoring a Location
288
Solution288
Recipe 8.4 Using a BroadcastReceiver to Obtain Locations
289
Solution290
Recipe 8.5 Location Data Logging
293
Solution293
Chapter 9: Accessing the Hardware
Recipe 9.1 Capturing Pictures with the Camera
301
301
Solution301
Recipe 9.2 Detecting the Presence of Hardware Features
304
Solution305
Recipe 9.3 Checking Network State
306
Solution306
Recipe 9.4 Turning GPS On/Off
308
Solution308
Recipe 9.5 Capturing Hardware Buttons Programmatically
310
Solution310
Recipe 9.6 Switching on the Flashlight
313
Solution314
Recipe 9.7 Capturing Barcodes
319
Solution320
Chapter 10: Persisting Data
Recipe 10.1 Saving and Loading User Preferences
325
325
Solution326
Recipe 10.2 Creating a Preference Screen
328
Solution328
Recipe 10.3 Saving Files to the Data Directory
333
Solution333
Recipe 10.4 Saving Files to the Cache Directory
338
Solution338
Recipe 10.5 Saving Files to External Storage
340
Solution341
xiii
CONTENTS
Recipe 10.6 Attaching Files to Your Project
345
Solution346
Recipe 10.7 Creating and Using SQLite Databases Programmatically 347
Solution348
Recipe 10.8 Pre-creating the SQLite Databases
353
Solution353
Chapter 11: Deploying Your Android Applications
Recipe 11.1 Localizing Your Application
357
357
Solution358
Recipe 11.2 Exporting Your Application as an APK File
362
Solution362
Recipe 11.3 Deploying Your App Through E‑mail
365
Solution365
Recipe 11.4 Deploying Your App Through the Web
367
Solution368
Recipe 11.5 Deploying Your App Through an SD Card
369
Solution369
Recipe 11.6 Specifying the Application Installation Location
370
Solution371
Index
xiv
373
Introduction
The pace of Android development has been fast and furious. Within a short time span of a few
years, Android has matured into a stable platform, rivaling that of its main competitor, iOS. At the
time of writing, the latest version of Android is 4.1 (aka Jelly Bean). Android 4.1 runs on both smartphones and tablets, making it the platform of choice for many developers.
This book was born out of the many frustrations I have had when developing Android applications.
It is often the case that you just need a quick snapshot showing how to do a certain task, and a code
snippet would be a quick fix. However, a trip to the official Android documentation often caused
more confusion than help, as the code samples are not always complete. Hence, this book aims to
fill the void by providing standalone examples that you can quickly “embrace and extend.”
Each recipe tackles a problem that you might face in your daily life as an Android developer —
whether it’s as minor as using a Button view or as involved as implementing a Master-Detail application using fragments. You can read this book from the first recipe until the last recipe, or you can
turn directly to the recipes that interest you most.
Who This Book Is For
This book is targeted at Android programmers who already have some basic knowledge of creating
Android applications. It is assumed that you know how to create an Android project using Eclipse,
and that you are already familiar with the structure of an Android project.
All the code samples in this book were written and tested using the Android 4.1 SDK, together with
Eclipse (Juno release) and the ADT plugin 20.0.3. All projects are able to run on Android devices
beginning with Android version 2.2. In particular, all projects make use of the Android Support package that is by default included in Android 4.1 projects. Using the Android Support Package enables
your applications to make use of the newer features introduced with Android version 3.0 (such as
fragments) and still run on older Android devices.
NOTE While every effort has been made to ensure that all the tools used in the
examples are the latest, it is always possible that by the time you read this book,
newer versions may be available. If so, some of the instructions and/or screenshots may differ slightly. However, any variations should be manageable.
introduction
What This Book Covers
This book covers all key areas of Android programming using the Android 4.1 SDK. It is divided
into 11 chapters.
Chapter 1: Android Fundamentals covers basic topics such as how to link activities, pass data
between activities, send and receive broadcasts, call built-in apps, and more.
Chapter 2: Using Views to Design the User Interface explains how to use the various views to build
the user interface of your Android applications. Also covered are the different types of layouts supported in Android to arrange the views, including LinearLayout, RelativeLayout, FrameLayout, and
others. You will also learn how to display context and option menus.
Chapter 3: Displaying Lists of Items and Images covers how to use the ListView and Spinner, and
how to customize them to display a list of items. It also demonstrates how to use fragments to create
Master-Detail applications.
Chapter 4: Telephony covers topics related to the phone on your Android device, such as how to
block outgoing calls, auto-answering incoming calls, enabling Bluetooth, and more.
Chapter 5: Messaging covers how to send and intercept SMS messages on your Android phone. You
will also learn how to monitor for SMS messages sent by your users.
Chapter 6: Network Programming covers topics related to getting your Android application connected to the outside world. You will learn about how to consume XML and JSON web services,
sockets programming, and Bluetooth communications.
Chapter 7: Using Google Maps includes topics about how to display Google Maps in your Android
application, how to perform reverse geocoding, and more.
Chapter 8: Location-Based Data Services covers the key techniques you need to know to build
location-based services. You will also learn how to implement location data logging.
Chapter 9: Accessing the Hardware covers how to access the many hardware features from your
Android application. It includes recipes demonstrating how to take pictures using the built-in camera, how to turn GPS on/off, as well as how to enable the device’s flashlight.
Chapter 10: Persisting Data covers several methods for persisting your data, including internal storage, external storage, a database, and more.
Chapter 11: Deploying your Android Applications covers the different ways to deploy your Android
applications, such as through an SD card, a web server, or e‑mail.
xvi
introduction
How This Book Is Structured
Chapters in this book are divided into main topics, with each chapter containing multiple “recipes”
that address specific subtopics in more detail. Instead of adopting the step-by-step approach of
creating a project and then explaining how the code works, this books demonstrates the key ingredients of each recipe — the key points you need to understand in order to meet a requirement or
solve a problem (or complete a common task) in Android programming. Each recipe covers the core
concepts you need to understand, without any unnecessary code that can complicate an example.
Using this approach, it would be easiest for you to copy-and-paste the code into your own project
and then enhance it for your own purpose. This, in my opinion, is the best way to learn Android
programming.
To that end, every chapter has a very defined structure. Each recipe is numbered and has a title. The
recipe begins with a list of components needed to successfully complete the solution, like so:
Recipe 0.0 the recipe requirements
Android Versions
Each recipe uses APIs from the Android SDK. The Android Versions section
states the version (level number) from which the APIs are from. For example,
you might see “Level 1 and above.” This indicates that the APIs used in this
recipe are available from Android level 1 (i.e., version 1.0) and above.
Permissions
The Permissions section shows the permissions that you need to add into
your application, specifically the AndroidManifest.xml file, in order to use
the APIs described in the recipe. Be sure to add the permission; forgetting to
do so will usually cause the application to crash during run time.
Source Code to Download from Wrox.com
The Source Code section shows the name of the ZIP file that you can download from the support web site of this book from Wrox.com. The ZIP file
contains the complete project used to illustrate the concept in that recipe. If
you want to quickly get the code for the recipe into your own project, downloading the source code is your fastest option.
Then, the main goal of the recipe is explained. Next the solution follows. Sometimes, the solution is
short and sweet, and sometimes the solution is more complex and requires multiple steps.
xvii
introduction
A Note About Including Permissions in Code
To make the recipes succinct and easy to follow, this book assumes that you know
how to add permissions in your application. For example, instead of listing the entire
AndroidManifest.xml file and highlighting the permissions that you need to add,
like this:
<manifest xmlns:android=" />package="net.learn2develop.http"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
android:name="android.permission.ACCESS_NETWORK_STATE"/>
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
… the recipe will simply indicate that you need to add the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
android:name="android.permission.ACCESS_NETWORK_STATE"/>
In the event that the above step is not explicitly mentioned, you should always
check the Permissions section at the beginning of each recipe and add the
permission(s) to the AndroidManifest.xml file.
xviii
introduction
What You Need to Use This Book
Most of the examples in this book run on the Android emulator, which is included as part of the
Android SDK. However, to get the most out of this book, using a real Android device is recommended (though not absolutely necessary).
Additionally, each recipe begins with a list of requirements specific to that recipe (versions, permissions, and source code), as previously discussed.
Conventions
To help you get the most from the text and keep track of what’s happening, a number of conventions
are used throughout the book:
➤➤
New terms and important words are highlighted in italics when first introduced.
➤➤
Keyboard combinations are treated like this: Ctrl+R.
➤➤
Filenames, URLs, and code within the text are treated like so: persistence.properties.
➤➤
Code is presented in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bolding to emphasize code that is of particular importance in the
present context.
NOTE Notes, tips, hints, tricks, and asides to the current discussion look like
this.
Source Code
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book
is available for download at www.wrox.com. When at the site, simply locate the book’s title (use the
Search box or one of the title lists) and click the Download Code link on the book’s detail page to
obtain all the source code for the book.
After you download the code, just decompress it with your favorite compression tool. Alternatively,
go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see
the code available for this book and all other Wrox books.
xix
introduction
NOTE Because many books have similar titles, you may find it easiest to search
by ISBN; this book’s ISBN is 978-1-118-17767-9.
Errata
We make every effort to ensure that there are no errors in the text or in the code. However, no one
is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake
or a faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may
save another reader hours of frustration and at the same time help us provide even higher-quality
information.
To find the errata page for this book, go to www.wrox.com and locate the title using the Search box
or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you
can view all errata that has been submitted for this book and posted by Wrox editors. A complete
book list, including links to each book’s errata, is also available at www.wrox.com/misc-pages/
booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport
.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent
editions of the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based
system for you to post messages relating to Wrox books and related technologies and to interact with
other readers and technology users. The forums offer a subscription feature to e‑mail you topics of
interest of your choosing when new posts are made to the forums. Wrox authors, editors, other
industry experts, and your fellow readers are present on these forums.
At p2p.wrox.com, you will find a number of different forums that will help you not only as you
read this book but also as you develop your own applications. To join the forums, just follow
these steps:
Go to p2p.wrox.com and click the Register link.
1.
2.
3.
4.
You will receive an e‑mail with information describing how to verify your account and
complete the joining process.
xx
Read the terms of use and click Agree.
Complete the required information to join as well as any optional information you want to
provide and click Submit.
introduction
NOTE You can read messages in the forums without joining P2P, but in order to
post your own messages you must join.
After you join, you can post new messages and respond to messages that other users post. You can
read messages at any time on the Web. If you want to have new messages from a particular forum
e‑mailed to you, click the Subscribe to This Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to
questions about how the forum software works, as well as for many common questions specific to
P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
xxi
1
Android Fundamentals
In this chapter, you learn about the fundamental topics in Android that most developers need to
know, including how to link to other applications using the Intent object, how to communicate
with other applications (or parts of the same application) using broadcast receivers, and how
to pass data between activities.
RECIPE 1.1 LINKING ACTIVITIES
Android Versions
Level 1 and above
Permissions
None
Source Code to Download at Wrox.com
Linking.zip
Unless you are writing a Hello World application, chances are good that your application contains several activities that you need to connect in order to form a cohesive application. This
recipe shows you the various ways to link to another activity in your Android application.