Home > Store

Android Developer's Cookbook, The: Building Applications with the Android SDK: Building Applications with the Android SDK

Register your product to gain access to bonus material or receive a coupon.

Android Developer's Cookbook, The: Building Applications with the Android SDK: Building Applications with the Android SDK


  • Sorry, this book is no longer in print.
Not for Sale


  • Copyright 2011
  • Edition: 1st
  • Book
  • ISBN-10: 0-321-74123-4
  • ISBN-13: 978-0-321-74123-3

Want to get started building applications for Android, the world’s hottest, fast-growing mobile platform? Already building Android applications and want to get better at it? This book brings together all the expert guidance–and code–you’ll need!

Completely up-to-date to reflect the newest and most widely used Android SDKs, The Android Developer’s Cookbook is the essential resource for developers building apps for any Android device, from phones to tablets.

Proven, modular recipes take you from the absolute basics to advanced location-based services, security techniques, and performance optimization. You’ll learn how to write apps from scratch, ensure interoperability, choose the best solutions for common problems, and avoid development pitfalls. Coverage includes:

  • Implementing threads, services, receivers, and other background tasks
  • Providing user alerts
  • Organizing user interface layouts and views
  • Managing user-initiated events such as touches and gestures
  • Recording and playing audio and video
  • Using hardware APIs available on Android devices
  • Interacting with other devices via SMS, web browsing, and social networking
  • Storing data efficiently with SQLite and its alternatives
  • Accessing location data via GPS
  • Using location-related services such as the Google Maps API
  • Building faster applications with native code
  • Providing backup and restore with the Android Backup Manager
  • Testing and debugging apps throughout the development cycle

Turn to The Android Developer’s Cookbook for proven, expert answers–and the code you need to implement them. It’s all you need to jumpstart any Android project, and create high-value, feature-rich apps that sell!



Download the examples

Sample Content

Online Sample Chapter

Android Application Basics: Activities and Intents

Table of Contents

1 Overview of Android 1

                The Evolution of Android 1

                The Dichotomy of Android 2

                Devices Running Android 2

                                HTC Models 6

                                Motorola Models 6

                                Samsung Models 6

                                Tablets 7

                                Other Devices 7

                Hardware Differences on Android Devices 8

                                Screens 8

                                User Input Methods 9

                                Sensors 9

                Features of Android 10

                                Multiprocess and App Widgets 11

                                Touch, Gestures, and Multitouch 11

                                Hard and Soft Keyboards 11

                Android Development 11

                                How to Use the Recipes in This Book 12

                                Designing Applications Well 12

                                Maintaining Forward Compatibility 13

                                Robustness 13

                Software Development Kit 14

                                Installing and Upgrading 14

                                Software Features and API Level 15

                                Emulator and Android Device Debug 16

                                Using the Android Debug Bridge 18

                                Signing and Publishing 18

                Android Market 19

                                End-User License Agreement 19

                                Improving App Visibility 19

                                Differentiating an App 20

                                Charging for an App 20

                                Managing Reviews and Updates 21

                                Alternatives to the Android Market 22

2 Application Basics: Activities and Intents 23

                Android Application Overview 23

                                Recipe: Creating a Project and an Activity 24

                                Directory Structure of Project and Autogenerated

                                Content 26

                                Android Package and Manifest File 28

                                Renaming Parts of an Application 30

                Activity Lifecycle 30

                                Recipe: Utilizing Other Lifecycle Functions 31

                                Recipe: Forcing Single Task Mode 33

                                Recipe: Forcing Screen Orientation 34

                                Recipe: Saving and Restoring Activity Information 34

                Multiple Activities 35

                                Recipe: Using Buttons and TextView 36

                                Recipe: Launching Another Activity from an Event 37

                                Recipe: Launching an Activity for a Result Using Speech to Text 41

                                Recipe: Implementing a List of Choices 43

                                Recipe: Using Implicit Intents for Creating an Activity 44

                                Recipe: Passing Primitive Data Types Between Activities 46

3 Threads, Services, Receivers, and Alerts 51

                Threads 51

                                Recipe: Launching a Secondary Thread 51

                                Recipe: Creating a Runnable Activity 55

                                Recipe: Setting a Thread’s Priority 57

                                Recipe: Canceling a Thread 57

                                Recipe: Sharing a Thread Between Two

                                Applications 58

                Messages Between Threads: Handlers 58

                                Recipe: Scheduling a Runnable Task from the Main Thread 59

                                Recipe: Using a Countdown Timer 61

                                Recipe: Handling a Time-Consuming Initialization 62

                Services 64

                                Recipe: Creating a Self-Contained Service 65

                Adding a Broadcast Receiver 69

                                Recipe: Starting a Service When the Camera Button Is

                                Pressed 70

                App Widgets 72

                                Recipe: Creating an App Widget 72

                Alerts 74

                                Recipe: Using Toast to Show a Brief Message on the

                                Screen 74

                                Recipe: Using an Alert Dialog Box 75

                                Recipe: Showing Notification in Status Bar 76

4 User Interface Layout 79

                Resource Directories and General Attributes 79

                                Recipe: Specifying Alternate Resources 81

                Views and ViewGroups 82

                                Recipe: Building Layouts in the Eclipse Editor 83

                                Recipe: Controlling the Width and Height of UI

                                Elements 86

                                Recipe: Setting Relative Layout and Layout ID 89

                                Recipe: Declaring a Layout Programmatically 90

                                Recipe: Updating a Layout from a Separate

                                Thread 92

                Text Manipulation 94

                                Recipe: Setting and Changing Text Attributes 95

                                Recipe: Providing Text Entry 98

                                Recipe: Creating a Form 100

                Other Widgets: From Buttons to Seek Bars 101

                                Recipe: Using Image Buttons in a Table Layout 102

                                Recipe: Using Check Boxes and Toggle Buttons 105

                                Recipe: Using Radio Buttons 108

                                Recipe: Creating a Drop-Down Menu 110

                                Recipe: Using a Progress Bar 112

                                Recipe: Using a SeekBar 114

5 User Interface Events 117

                Event Handlers and Event Listeners 117

                                Recipe: Intercepting a Physical Key Press 117

                                Recipe: Building Menus 121

                                Recipe: Defining Menus in XML 126

                                Recipe: Utilizing the SEARCH Key 127

                                Recipe: Reacting to Touch Events 128

                                Recipe: Listening for Fling Gestures 130

                                Recipe: Using Multitouch 133

                Advanced User Interface Libraries 136

                                Recipe: Using Gestures 136

                                Recipe: Drawing 3D Images 140

6 Multimedia Techniques 147

                Images 148

                                Recipe: Loading an Image for Manipulation 148

                Audio 154

                                Recipe: Choosing and Playing Back Audio Files 154

                                Recipe: Recording Audio Files 157

                                Recipe: Manipulating Raw Audio 158

                                Recipe: Using Sound Resources Efficiently 163

                                Recipe: Adding Media and Updating Paths 165

                Video 165

7 Hardware Interface 169

                Camera 169

                                Recipe: Customizing the Camera 170

                Other Sensors 175

                                Recipe: Getting a Device’s Rotational Attitude 176

                                Recipe: Using the Temperature and Light Sensor 179

                Telephony 180

                                Recipe: Utilizing the Telephony Manager 181

                                Recipe: Listening for Phone States 183

                                Recipe: Dialing a Phone Number 185

                Bluetooth 185

                                Recipe: Turning on Bluetooth 186

                                Recipe: Discovering Bluetooth Devices 187

                                Recipe: Pairing with Bonded Bluetooth Devices 188

                                Recipe: Opening a Bluetooth Socket 188

                                Recipe: Using Device Vibration 191

                                Recipe: Accessing the Wireless Network 191

8 Networking 195

                Using SMS 195

                                Recipe: Autosend an SMS Based on a Received

                                SMS 197

                Using Web Content 204

                                Recipe: Customizing a Web Browser 204

                                Recipe: Using an HTTP GET 204

                                Recipe: Using HTTP POST 209

                Social Networking 210

                                Recipe: Integrating with Twitter 210

9 Data Storage Methods 221

                Shared Preferences 221

                                Recipe: Creating and Retrieving Shared

                                Preferences 222

                                Recipe: Using the Preferences Framework 222

                                Recipe: Changing the UI Based on Stored Data 225

                                Recipe: Adding a EULA 228

                SQLite Database 232

                                Recipe: Creating a Separate Database Package 232

                                Recipe: Using a Separate Database Package 236

                                Recipe: Creating a Personal Diary 239

                Content Provider 243

                                Recipe: Creating a Custom Content Provider 244

                File Saving and Loading 249

10 Location-Based Services 251

                Location Basics 251

                                Recipe: Retrieving Last Location 253

                                Recipe: Updating Location Upon Change 254

                                Recipe: Listing All Enabled Providers 256

                                Recipe: Translating a Location to Address (Reverse Geocoding) 258

                                Recipe: Translating an Address to Location (Geocoding) 261

                Using Google Maps 263

                                Recipe: Adding Google Maps to an Application 265

                                Recipe: Adding Markers on a Map 267

                                Recipe: Adding Views to a Map 271

                                Recipe: Marking the Device’s Current Location on a

                                Map 274

                                Recipe: Setting up a Proximity Alert 274

11 Advanced Android Development 277

                Android Custom View 277

                                Recipe: Customizing a Button 277

                Android Native Components 283

                                Recipe: Developing a Native Component 284

                Android Security 287

                                Recipe: Declaring and Enforcing Permissions 288

                Android Inter-Process Communication 288

                                Recipe: Implementing a Remote Procedure Call 289

                Android Backup Manager 294

                                Recipe: Creating a Backup of Runtime Data 294

                                Recipe: Backing Up Files to the Cloud 296

                                Recipe: Triggering Backup and Restore 296

                Android Animation 298

                                Recipe: Creating an Animation 299

12 Debugging 303

                Eclipse Built-in Debug Tools 303

                                Recipe: Specifying a Run Configuration 303

                                Recipe: Using the DDMS 304

                                Recipe: Debugging Through Breakpoints 306

                Android SDK Debug Tools 307

                                Recipe: Using the Android Debug Bridge 307

                                Recipe: Using LogCat 307

                                Recipe: Using the Hierarchy Viewer 309

                                Recipe: Using TraceView 311

                Android System Debug Tools 313

                                Recipe: Setting up GDB Debugging 315

TOC, 9780321741233, 9/25/2010


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020