Praise For Introduction To Android Application Development .

Transcription

Praise for Introduction to Android Application Development,Fifth Edition“Introduction to Android Application Development is a great resource for developers whowant to understand Android app development but who have little or no experience withmobile software. This fifth edition has a bunch of great changes, from using Android Studio to understanding and implementing navigation patterns, and each chapter has quiz questions to help make sure you’re picking up the vital info that fills this book.”—Ian G. Clifton, author of Android User Interface Design“Revamped, revitalized, and refreshed! Introduction to Android Application Development,Fifth Edition, is a wonderful upgrade to an already impressive compendium. Common pitfalls are explained, new features are covered in depth, and the knowledge that thebook is geared to cover everything from introduction of a concept to learning how to implement it into your app makes this a great choice for new developers who are ready tomake the jump into Android development. Being already accustomed to the professionalwork and experience that Annuzzi et al., bring to the table, you will be grateful to haveexpert insight along with the care and instruction that developers of all skill levels canbenefit from.”—Phil Dutson, solution architect, ICON Health & Fitness“Best technical summary of Material Design implementation I’ve seen outside the Android sample docs.”—Ray Rischpater, software development manager, Uber“Introduction to Android Application Development is well written and fulfills the requirementsof developers, project managers, educators, and entrepreneurs in developing fully featuredAndroid applications. In addition, it emphasizes quality assurance for mobile applications,teaches you how to design and plan your Android application, and teaches the softwaredevelopment process through a step-by-step, easy-to-understand approach. I recommendthis book to anyone who wants to not just focus on developing apps, but also to applytips and tricks and other tools for project management in their development of successfulapplications.”—Bintu Harwani, founder of MCE (Microchip Computer Education)

This page intentionally left blank

Introduction to AndroidApplicationDevelopmentFifth Edition

Developer’s Library SeriesVisit developers-library.com for a complete list of available productsThe Developer’s Library Series from Addison-Wesley providespracticing programmers with unique, high-quality references andtutorials on the latest programming languages and technologies theyuse in their daily work. All books in the Developer’s Library are written byexpert technology practitioners who are exceptionally skilled at organizingand presenting information in a way that’s useful for other programmers.Developer’s Library books cover a wide range of topics, from opensource programming languages and databases, Linux programming,Microsoft, and Java, to Web development, social networking platforms,Mac/iPhone programming, and Android programming.

Introduction to AndroidApplicationDevelopmentAndroid EssentialsFifth EditionJoseph Annuzzi, Jr.Lauren DarceyShane ConderBoston Columbus Indianapolis New York San Francisco Amsterdam Cape TownDubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico CitySao Paulo Sidney Hong Kong Seoul Singapore Taipei Tokyo

Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim,the designations have been printed with initial capital letters or in all capitals.The authors and publisher have taken care in the preparation of this book, but make no expressed or impliedwarranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidentalor consequential damages in connection with or arising out of the use of the information or programs containedherein.For information about buying this title in bulk quantities, or for special sales opportunities (which may includeelectronic versions; custom cover designs; and content particular to your business, training goals, marketingfocus, or branding interests), please contact our corporate sales department at corpsales@pearsoned.com or (800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Visit us on the Web: informit.com/awLibrary of Congress Cataloging-in-Publication DataNames: Annuzzi, Joseph, Jr., author. Darcey, Lauren, 1977- author. Conder, Shane, 1975- author.Title: Introduction to Android application development : Android essentials /Joseph Annuzzi, Jr., Lauren Darcey, Shane Conder.Description: Fifth edition New York : Addison-Wesley, [2016] Includesbibliographical references and index.Identifiers: LCCN 2015037913 ISBN 9780134389455 (pbk. : alk. paper)Subjects: LCSH: Application software—Development. Android (Electronicresource) Mobile computing. Wireless communication systems.Classification: LCC QA76.76.A65 A56 2016 DDC 005.3—dc23LC record available at http://lccn.loc.gov/2015037913Copyright 2016 Joseph Annuzzi, Jr., Lauren Darcey, and Shane ConderAll rights reserved. Printed in the United States of America. This publication is protected by copyright, andpermission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrievalsystem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, orlikewise. For information regarding permissions, request forms and the appropriate contacts within the PearsonEducation Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.Some figures that appear in this book have been reproduced from or are modifications based on work createdand shared by the Android Open Source Project and used according to terms described in the Creative Commons3.0 Attribution License (http://creativecommons.org/licenses/by/3.0/).Some figures that appear in this book have been reproduced from or are modifications based on work createdand shared by Google and used according to terms described in the Creative Commons Attribution 3.0 License.See shots of Google Products follow these -product-graphics.htmlThe following are registered trademarks of Google:Android , Chrome , Google Play , Nexus , Dalvik , Google Maps , Google , Google TV , Google andthe Google logo are registered trademarks of Google Inc.ARM is a registered trademark of ARM Limited (or its subsidiaries) in the EU and/or elsewhere. All rightsreserved.Altium and Altium Designer are trademarks or registered trademarks of Altium Limited or its subsidiaries.Qualcomm and Snapdragon are trademarks of Qualcomm Incorporated, registered in the United States and othercountries.Cyanogen is a trademark of Cyanogen Inc., registered in certain countries.CyanogenMod is a trademark of CyanogenMod, LLC, registered in the United States.JetBrains and IntelliJ , are registered trademarks owned by JetBrains s.r.o.ISBN-13: 978-0-13-438945-5ISBN-10: 0-13-438945-XText printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.First printing December 2015Editor-in-ChiefMark L. TaubExecutive EditorLaura LewinDevelopment EditorSonglin QiuManaging EditorJohn FullerSenior Project EditorKesel WilsonCopy EditorDeborah ThompsonIndexerJack LewisProofreaderSue BoshersTechnical ReviewersDouglas JonesRay RischpaterValerie ShipbaughEditorial AssistantOlivia BasegioCover DesignerChuti PrasertsithCompositorcodeMantra

vThis book is dedicated to Cleopatra (Cleo).—Joseph Annuzzi, Jr.This book is dedicated to ESC.—Lauren Darcey and Shane Conderv

This page intentionally left blank

Contents at a GlanceAcknowledgments  xxxiAbout the Authors  xxxiiiIntroduction  1I: Platform Overview1 Presenting Android  112 Setting Up for Development  313 Creating Your First Application  51II: Application Basics4 Understanding Application Components  775 Defining the Manifest  956 Managing Application Resources  1117 Exploring Building Blocks  1478 Positioning with Layouts  1799 Partitioning with Fragments  213III: Application Design Essentials10 Architecting with Patterns  23711 Appealing with Style  26912 Embracing Material Design  28513 Designing Compatible Applications  319IV: Application Development Essentials14 Using Android Preferences  34515 Accessing Files and Directories  36316 Saving with SQLite  37717 Leveraging Content Providers  393

xContents at a GlanceV: Application Delivery Essentials18 Learning the Development Workflow  41119 Planning the Experience  43720 Delivering Quality Applications  44921 Testing Your Applications  46722 Distributing Your Applications  499VI: AppendixesA Tips and Tricks: Android Studio  527B Quick-Start: Android Emulator  539C Quick-Start: Android Device Monitor  567D Mastery: Android SDK Tools  585E Quick-Start: Gradle Build System  603F Answers to Quiz Questions  623Index  631

ContentsAcknowledgments  xxxiAbout the Authors  xxxiiiIntroduction  1Who Should Read This Book 1Key Questions Answered in This Book 2How This Book Is Structured2An Overview of Changes in This Edition 3Development Environments Used in This Book 5Supplementary Materials for This Book 6Conventions Used in This Book 6Where to Find More Information 7Contacting the Authors 8I: Platform Overview1 Presenting Android  11The Android Open Source Project (AOSP) 11The Open Handset Alliance 12Google Goes Mobile First 12Introducing the Open Handset Alliance 12Joining the Open Handset Alliance 13Manufacturers: Designing Android Devices 13Mobile Operators: Delivering the AndroidExperience 14Apps Drive Device Sales: Developing AndroidApplications 14Taking Advantage of All Android Has to Offer 15Android: Where We Are Now 15Android Platform Uniqueness 16Android: The Code Names 16Free and Open Source 17Familiar and Inexpensive Development Tools 17Reasonable Learning Curve for Developers 18Enabling Development of Powerful Applications 18Rich, Secure Application Integration 19

xiiContentsNo Costly Obstacles for Development 19A “Free Market” for Applications 19A Growing PlatformThe Android Platform2021Android’s Underlying Architecture 21Security and Permissions 22Exploring Android Applications 23Android beyond the OHA and GMS 26Amazon Fire OS 26Cyanogen OS and CyanogenMod 27Maker Movement and Open-Source Hardware 27Maintaining Awareness 28Summary28Quiz Questions 28Exercises 28References and More Information 292 Setting Up for Development  31Configuring Your Development Environment 31Configuring Your Operating System for DeviceDebugging 34Configuring Your Android Hardware for Debugging 34Upgrading Android Studio 35Upgrading the Android SDK 37Problems with Android StudioProblems with the Android SDK3737IntelliJ IDEA as an Android Studio Alternative 38Exploring the Android SDK 38Understanding the Android SDK LicenseAgreement 38Reading the Android SDK Documentation 40Exploring the Core Android ApplicationFramework 40Exploring the Core Android Tools 42Exploring the Android Sample Applications 45Summary 48Quiz Questions 48Exercises 48References and More Information 48

Contents3 Creating Your First Application  51Testing Your Development Environment 51Importing the BorderlessButtons Sample intoAndroid Studio 52Using the Preinstalled AVD for Running YourBorderlessButtons Project 54Running the BorderlessButtons Applicationin the Android Emulator 55Building Your First Android Application 57Creating and Configuring a New Android Project 57Understanding the Android Symbolic Viewand the Traditional Project View 62Core Files and Directories of the AndroidApplication 62Running Your Android Application in the Emulator 64Debugging Your Android Application in theEmulator 66Adding Logging Support to Your AndroidApplication 69Debugging Your Application on Hardware 71Summary73Quiz Questions 74Exercises74References and More Information 74II: Application Basics4 Understanding Application Components  77Mastering Important Android Terminology 77The Application Context78Retrieving the Application ContextUsing the Application Context7878Performing Application Tasks with Activities 80The Lifecycle of an Android Activity80Organizing Activity Components with Fragments 85Managing Activity Transitions with Intents 87Transitioning between Activities with Intents 88Organizing Application Navigation with Activities,Fragments, and Intents 90xiii

xivContentsWorking with Services 90Receiving and Broadcasting Intents 91Summary92Quiz Questions 92Exercises 93References and More Information 935 Defining the Manifest  95Configuring Android Applications Using the AndroidManifest File 95Editing the Android Manifest File 96Managing Your Application’s Identity 99Setting the Application Name and Icon 99Enforcing Application System Requirements 100Enforcing Application Platform Requirements 100Other Application-Configuration Settings andFilters 102Registering Activities in the Android Manifest 103Designating a Primary Entry-Point Activity forYour Application Using an Intent Filter 103Configuring Other Intent Filters 103Registering Other Application Components 104Working with Permissions105Registering Permissions Your ApplicationRequires 105Registering Permissions Your ApplicationEnforces 108Exploring Other Manifest File Settings 109Summary 109Quiz Questions 109Exercises 110References and More Information 1106 Managing Application Resources  111What Are Resources? 111Storing Application Resources 111Resource Value Types 112Accessing Resources Programmatically 116Adding Simple Resource Valuesin Android Studio 116

ContentsWorking with Different Types of Resources 120Working with String Resources 120Using String R

Fifth Edition “Introduction to . tips and tricks and other tools for project management in their development of successful applications.” —Bintu Harwani, founder of MCE (Microchip Computer Education) This page intentionally left blank . Introduction to Android Application Development Fifth Edition. The Developer’s Library Series from Addison-Wesley provides practicing programmers .