Pro Android 5 Dave MacLean Satya Komatineni Grant Allen Pro Android 5 Copyright © 2015 by Dave MacLean, Satya Komatineni, and Grant Allen This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-4302-4680-0 ISBN-13 (electronic): 978-1-4302-4681-7 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The images of the Android Robot (01/Android Robot) are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. Android and all Android and Google-based marks are trademarks or registered trademarks of Google Inc. in the United States and other countries. Apress Media LLC is not affiliated with Google Inc., and this book was written without endorsement from Google Inc. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Shane Kirk Editorial Board: Steve Anglin, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss Coordinating Editor: Mark Powers Copy Editor: Brendan Frost Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com/9781430246800. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. To my wife Rosie, I never would have made it this far without your love and support. Thank you, you are wonderful, and I love you. —Dave MacLean To my late younger brother Sankar Komatineni whose industry, hardship, and zest for life fills me with sadness and joy. —Satya Komatineni To all the Android developers out there dreaming of the next great Android app! You are an amazing community, and I can’t wait to see what you develop next. —Grant Allen Contents at a Glance About the Authors About the Technical Reviewer Acknowledgments Foreword Introduction Chapter 1: Hello Android Chapter 2: Introduction to Android Application Architecture Chapter 3: Building Basic User Interfaces and Using Controls Chapter 4: Adapters and List Controls Chapter 5: Building More Advanced UI Layouts Chapter 6: Working with Menus and Action Bars Chapter 7: Styles and Themes Chapter 8: Fragments Chapter 9: Responding to Configuration Changes Chapter 10: Working with Dialogs Chapter 11: Working with Preferences and Saving State Chapter 12: Using the Compatibility Library for Older Devices Chapter 13: Exploring Packages, Processes, Threads, and Handlers Chapter 14: Building and Consuming Services Chapter 15: Advanced AsyncTask and Progress Dialogs Chapter 16: Broadcast Receivers and Long-Running Services Chapter 17: Exploring the Alarm Manager Chapter 18: Exploring 2D Animation Chapter 19: Exploring Maps and Location-Based Services Chapter 20: Understanding the Media Frameworks Chapter 21: Home Screen Widgets Chapter 22: Touch Screens Chapter 23: Implementing Drag and Drop Chapter 24: Using Sensors Chapter 25: Exploring Android Persistence and Content Providers Chapter 26: Understanding Loaders Chapter 27: Exploring the Contacts API Chapter 28: Exploring Security and Permissions Chapter 29: Using Google Cloud Messaging with Android Chapter 30: Deploying Your Application: Google Play Store and Beyond Index Contents About the Authors About the Technical Reviewer Acknowledgments Foreword Introduction Chapter 1: Hello Android Prerequisites for Android Development Setting Up Your Eclipse Environment Downloading JDK 6 Downloading Eclipse 4.2 Downloading the Android SDK The Tools Window Installing ADT Setting Up Your Android Studio Environment Java requirements for Android Studio Downloading and Installing Android Studio Learning Android’s Fundamental Components View Activity Fragment Intent Content Provider Service AndroidManifest.xml AVDs Hello World! AVDs Running on a Real Device Exploring the Structure of an Android Application Examining the Application Life Cycle Simple Debugging Launching the Emulator References Summary Chapter 2: Introduction to Android Application Architecture Exploring a Simple Android Application Defining UI through Layout Files Specifying Comments in Layout Files Adding Views and View Groups in Layout Files Specifying Control Properties in Layout Files Indicating ViewGroup Properties Controlling Width and Height of a Control Introducing Resources and Backgrounds Working with Text Controls in the Layout File Working with Autogenerated IDs for Controls Implementing Programming Logic Loading the Layout File into an Activity Gathering Controls Setting Up Buttons Responding to Button Clicks: Tying It All Together Updating the AndroidManifest.XML Placing the Files in the Android Project Testing the Calculator App on a Real Device Android Activity Life Cycle void onCreate(Bundle savedInstanceState) void onStart() void onRestoreInstanceState(Bundle savedInstanceState) void onResume() void onPause() void onStop() void onSaveInstanceState(Bundle saveStateBundle) void onRestart() Object onRetainNonConfigurationInstance() void onDestroy() General Notes on Activity Callbacks More on Resources Directory Structure of Resources Reading Resources from Java Code Runtime Behavior of Drawable Resources Using Arbitrary XML Files as Resources Working with Raw Resource Files Reading Files from the Assets Directory Reading Resources and Assets Without an Activity Reference Understanding Resource Directories, Language, and Locale More on Intents Starting Activities for Results Exercising the GET_CONTENT Action Relating Intents and Activities Understanding Explicit and Implicit Intents Saving State in Android Roadmap for Learning Android and the Rest of the Book Track 1: UI Essentials for Your Android Applications Track 2: Saving State Track 3: Preparing/Taking Your Application to the Market Track 4: Making Your Application Robust Track 5: Bringing Finesse to Your Apps Track 6: Integrating with Other Devices and the Cloud Final Track: Getting a Helping Hand from Expert Android As We Leave You Now with the Rest of the Book References Summary Chapter 3: Building Basic User Interfaces and Using Controls UI Development in Android Building a UI Completely in Code Building a UI Completely in XML Building a UI in XML with Code Understanding Android’s Common Controls Text Controls Button Controls The ImageView Control Date and Time Controls The MapView Control References
Description: