You are previewing Advanced Android™ Application Development, Fourth Edition.
O'Reilly logo
Advanced Android™ Application Development, Fourth Edition

Book Description

Advanced Android™ Application Development, Fourth Edition, is the definitive guide to building robust, commercial-grade Android apps. Systematically revised and updated, this guide brings together powerful, advanced techniques for the entire app development cycle, including design, coding, testing, debugging, and distribution. With the addition of quizzes and exercises in every chapter, it is ideal for both professional and classroom use.

An outstanding practical reference for the newest Android APIs, this guide provides in-depth explanations of code utilizing key API features and includes downloadable sample apps for nearly every chapter. Together, they provide a solid foundation for any modern app project.

Throughout, the authors draw on decades of in-the-trenches experience as professional mobile developers to provide tips and best practices for highly efficient development. They show you how to break through traditional app boundaries with optional features, including the Android NDK, Google Analytics and Android Wear APIs, and Google Play Game Services.

New coverage in this edition includes

  • Integrating Google Cloud Messaging into your apps

  • Utilizing the new Google location and Google Maps Android APIs

  • Leveraging in-app billing from Google Play, as well as third-party providers

  • Getting started with the Android Studio IDE

  • Localizing language and using Google Play App Translation services

  • Extending your app’s reach with Lockscreen widgets and DayDreams

  • Leveraging improvements to Notification, Web, SMS, and other APIs 

  • Annuzzi has released new source code samples for use with Android Studio. The code updates are posted to the associated blog site: http://advancedandroidbook.blogspot.com/

    This title is an indispensable resource for intermediate- to advanced-level Java programmers who are now developing for Android, and for seasoned mobile developers who want to make the most of the new Android platform and hardware.

    This revamped, newly titled edition is a complete update of Android™ Wireless Application Development, Volume II: Advanced Topics, Third Edition.

    Table of Contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. Praise for Advanced Android™ Application Development, Fourth Edition
    5. Dedication Page
    6. Contents at a Glance
    7. Contents
    8. Acknowledgments
    9. About the Authors
    10. Introduction
      1. Who Should Read This Book?
      2. How This Book Is Structured
      3. Key Questions Answered in This Book
      4. An Overview of Changes in This Edition
      5. The Development Environment Used in This Book
      6. Supplementary Materials Available
      7. Where to Find More Information
      8. Conventions Used in This Book
      9. Contacting the Authors
    11. I: Advanced Android Application Design Principles
      1. 1. Threading and Asynchronous Processing
        1. The Importance of Processing Asynchronously
        2. Working with the AsyncTask Class
        3. Working with the Thread Class
        4. Working with Loaders
        5. Understanding StrictMode
        6. Summary
        7. Quiz Questions
        8. Exercises
        9. References and More Information
      2. 2. Working with Services
        1. Determining When to Use Services
        2. Understanding the Service Lifecycle
        3. Creating a Service
        4. Controlling a Service
        5. Implementing a Remote Interface
        6. Implementing a Parcelable Class
        7. Using the IntentService Class
        8. Summary
        9. Quiz Questions
        10. Exercises
        11. References and More Information
      3. 3. Leveraging SQLite Application Databases
        1. Storing Structured Data Using SQLite Databases
          1. Creating a SQLite Database
          2. Creating, Updating, and Deleting Database Records
          3. Working with Transactions
        2. Querying SQLite Databases
          1. Working with Cursors
          2. Executing Simple Queries
          3. Executing More Complex Queries Using SQLiteQueryBuilder
          4. Executing Raw Queries without Builders and Column Mapping
        3. Closing and Deleting a SQLite Database
          1. Deleting Tables and Other SQLite Objects
          2. Closing a SQLite Database
          3. Deleting a SQLite Database Instance Using the Application Context
        4. Designing Persistent Databases
          1. Keeping Track of Database Field Names
          2. Extending the SQLiteOpenHelper Class
        5. Binding Data to the Application User Interface
          1. Working with Database Data Like Any Other Data
          2. Binding Data to Controls Using Data Adapters
        6. Summary
        7. Quiz Questions
        8. Exercises
        9. References and More Information
      4. 4. Building Android Content Providers
        1. Acting as a Content Provider
          1. Implementing a Content Provider Interface
          2. Defining the Data URI
          3. Defining Data Columns
          4. Implementing Important Content Provider Methods
          5. Updating the Manifest File
        2. Enhancing Applications Using Content Providers
        3. Summary
        4. Quiz Questions
        5. Exercises
        6. References and More Information
      5. 5. Broadcasting and Receiving Intents
        1. Sending Broadcasts
          1. Sending Basic Broadcasts
          2. Sending Ordered Broadcasts
        2. Receiving Broadcasts
          1. Registering to Receive Broadcasts
          2. Handling Incoming Broadcasts from the System
        3. Securing Application Broadcasts
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
      6. 6. Working with Notifications
        1. Notifying the User
        2. A Word on Compatibility
        3. Notifying with the Status Bar
          1. Using the NotificationManager Service
          2. Creating a Simple Text Notification with an Icon
          3. Working with the Notification Queue
          4. Updating Notifications
          5. Clearing Notifications
        4. Vibrating the Phone
        5. Blinking the Lights
        6. Making Noise
        7. Customizing the Notification
        8. Expandable and Contractible Notifications
        9. Notification Priority
        10. Introducing the Notification Listener
        11. Designing Useful Notifications
        12. Summary
        13. Quiz Questions
        14. Exercises
        15. References and More Information
    12. II: Advanced Android User Interface Design Principles
      1. 7. Designing Powerful User Interfaces
        1. Following Android User Interface Guidelines
        2. Enabling Action Bars
          1. Building Basic Action Bars
          2. Customizing Your Action Bar
          3. Handling Application Icon Clicks on the Action Bar
          4. Working with Screens That Do Not Require Action Bars
          5. Contextual Action Mode
        3. Working with Styles
          1. Building Simple Styles
          2. Leveraging Style Inheritance
        4. Working with Themes
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      2. 8. Handling Advanced User Input
        1. Working with Textual Input Methods
          1. Working with Software Keyboards
          2. Working with Text Prediction and User Dictionaries
          3. Using the Clipboard Framework
        2. Handling User Events
          1. Listening for Touch Mode Changes
          2. Listening for Events on the Entire Screen
          3. Listening for Long Clicks
          4. Listening for Focus Changes
        3. Working with Gestures
          1. Detecting User Motions within a View
          2. Handling Common Single-Touch Gestures
          3. Handling Common Multitouch Gestures
          4. Making Gestures Look Natural
          5. Using the Drag-and-Drop Framework
        4. Handling Screen Orientation Changes
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      3. 9. Designing Accessible Applications
        1. Exploring the Accessibility Framework
        2. Leveraging Speech Recognition Services
        3. Leveraging Text-to-Speech Services
        4. Testing Application Accessibility
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      4. 10. Development Best Practices for Tablets, TVs, and Wearables
        1. Understanding Device Diversity
          1. Don’t Make Assumptions about Device Characteristics
          2. Designing Flexible User Interfaces
          3. Attracting New Types of Users
          4. Leveraging Alternative Resources
          5. Using Screen Space Effectively on Big Landscape Screens
        2. Developing Applications for Tablets
        3. Developing Applications for TV
          1. Working with Google TV
          2. Google TV Variations
        4. Developing Applications for Wearables
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
    13. III: Leveraging Common Android APIs
      1. 11. Using Android Networking APIs
        1. Understanding Mobile Networking Fundamentals
        2. Understanding StrictMode with Networking
        3. Accessing the Internet (HTTP)
          1. Reading Data from the Web
          2. Using HttpURLConnection
          3. Parsing XML from the Network
          4. Handling Network Operations Asynchronously
          5. Retrieving Android Network Status
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
      2. 12. Using Android Web APIs
        1. Browsing the Web with WebView
          1. Designing a Layout with a WebView Control
          2. Loading Content into a WebView Control
          3. Adding Features to the WebView Control
          4. Managing WebView State
        2. Building Web Extensions
          1. Browsing the WebKit APIs
          2. Extending Web Application Functionality to Android
        3. Debugging WebViews with Chrome DevTools
        4. Working with Adobe AIR and Flash
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      3. 13. Using Android Multimedia APIs
        1. Working with Multimedia
        2. Working with the Camera
          1. Capturing Still Images Using the Camera
          2. Configuring Camera Mode Settings
          3. Working with Common Camera Parameters
          4. Zooming the Camera
          5. Sharing Images
          6. Assigning Images as Wallpapers
          7. Choosing among Various Device Cameras
        3. Working with Video
          1. Recording Video
          2. Playing Video
        4. Working with Face Detection
        5. Working with Audio
          1. Recording Audio
          2. Playing Audio
          3. Sharing Audio
        6. Searching for Multimedia
        7. Working with Ringtones
        8. Introducing the Media Router
        9. Summary
        10. Quiz Questions
        11. Exercises
        12. References and More Information
      4. 14. Using Android Telephony APIs
        1. Working with Telephony Utilities
          1. Gaining Permission to Access Phone State Information
          2. Requesting Call State
          3. Requesting Service Information
          4. Monitoring Signal Strength and Data Connection Speed
          5. Working with Phone Numbers
        2. Using SMS
          1. Default Messaging Application
          2. SMS Provider
          3. SMS Applications Other than the Default
        3. Making and Receiving Phone Calls
          1. Making Phone Calls
          2. Receiving Phone Calls
        4. Working with SIP
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      5. 15. Accessing Android’s Hardware Sensors
        1. Interacting with Device Hardware
        2. Using the Device Sensors
          1. Working with Different Sensors
          2. Configuring the Android Manifest File for Sensors
          3. Acquiring a Reference to a Sensor
          4. Reading Sensor Data
          5. Calibrating Sensors
          6. Determining Device Orientation
          7. Finding True North
        3. Sensor Event Batching
        4. Monitoring the Battery
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      6. 16. Using Android’s Optional Hardware APIs
        1. Working with Bluetooth
          1. Checking for the Existence of Bluetooth Hardware
          2. Enabling Bluetooth
          3. Querying for Paired Devices
          4. Discovering Devices
          5. Establishing Connections between Devices
        2. Working with USB
          1. Working with USB Accessories
          2. Working as a USB Host
        3. Working with Android Beam
          1. Enabling Android Beam Sending
          2. Receiving Android Beam Messages
          3. Configuring the Manifest File for Android Beam
          4. Android Beam over Bluetooth
          5. Introducing Host Card Emulation
        4. Working with Wi-Fi
          1. Introducing Wi-Fi Direct
          2. Monitoring Wi-Fi State
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
    14. IV: Leveraging Google APIs
      1. 17. Using Location and Map APIs
        1. Incorporating Android Location APIs
          1. Using the Global Positioning System (GPS)
          2. Geocoding Locations
          3. Doing More with Android Location-Based Services
        2. Incorporating Google Location Services APIs
          1. Locating with the Fused Location Provider
          2. Doing More with Google Location Services
        3. Incorporating Google Maps Android API v2
          1. Mapping Locations
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
      2. 18. Working with Google Cloud Messaging
        1. An Overview of GCM
          1. Understanding GCM Message Flow
          2. Understanding the Limitations of the GCM Service
          3. Signing Up for GCM
          4. Incorporating GCM into Your Applications
          5. Exploring the GCM Sample Applications
        2. What Alternatives to GCM Exist?
        3. Summary
        4. Quiz Questions
        5. Exercises
        6. References and More Information
      3. 19. An Overview of In-App Billing APIs for Android
        1. What Is In-App Billing?
        2. Using In-App Billing
        3. Leveraging Google Play In-App Billing APIs
        4. Leveraging Amazon Appstore for Android In-App Purchasing APIs
        5. Leveraging PayPal Billing APIs
        6. Leveraging Other Billing APIs
        7. Summary
        8. Quiz Questions
        9. Exercises
        10. References and More Information
      4. 20. Enabling Application Statistics with Google Analytics
        1. Creating a Google Account for Analytics
        2. Adding the Library to Your Android IDE Project
        3. Collecting Data from Your Applications
        4. Logging Different Events
        5. Using the Google Analytics Dashboard
        6. Gathering E-commerce Information
          1. Logging E-commerce Events in Your Applications
          2. Reviewing E-commerce Reports
        7. Tracking Ad and Market Referrals
        8. Gathering Statistics
        9. Protecting Users’ Privacy
        10. Summary
        11. Quiz Questions
        12. Exercises
        13. References and More Information
      5. 21. An Overview of Google Play Game Services
        1. Getting Up and Running with Google Play Game Services
        2. Incorporating Google Play Game Services into Your Applications
        3. Understanding Achievements
        4. Understanding Leaderboards
        5. Saving Game Data with Cloud Save
        6. Introducing Multiplayer Gaming
        7. Understanding Antipiracy
        8. Summary
        9. Quiz Questions
        10. Exercises
        11. References and More Information
    15. V: Drawing, Animations, and Graphics Programming with Android
      1. 22. Developing Android 2D Graphics Applications
        1. Drawing on the Screen
          1. Working with Canvases and Paints
          2. Understanding the Canvas Object
          3. Understanding the Paint Object
        2. Working with Text
          1. Using Default Fonts and Typefaces
          2. Using Custom Typefaces
          3. Measuring Text Screen Requirements
        3. Working with Bitmaps
          1. Drawing Bitmap Graphics on a Canvas
          2. Scaling Bitmap Graphics
          3. Transforming Bitmaps Using Matrixes
          4. Bitmap Performance Optimizations
        4. Working with Shapes
          1. Defining Shape Drawables as XML Resources
          2. Defining Shape Drawables Programmatically
          3. Drawing Different Shapes
        5. Leveraging Hardware Acceleration Features
          1. Controlling Hardware Acceleration
          2. Fine-Tuning Hardware Acceleration
        6. Summary
        7. Quiz Questions
        8. Exercises
        9. References and More Information
      2. 23. Working with Animation
        1. Animating Your Applications
          1. Working with Drawable Animation
          2. Working with View Animations
          3. Working with Property Animation
        2. Working with Different Interpolators
        3. Animating Activity Launch
        4. State Animations with Scenes and Transitions
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      3. 24. Developing Android 3D Graphics Applications
        1. Working with OpenGL ES
          1. Leveraging OpenGL ES in Android
          2. Ensuring Device Compatibility
        2. Using OpenGL ES APIs in the Android SDK
        3. Handling OpenGL ES Tasks Manually
          1. Creating a SurfaceView
          2. Starting Your OpenGL ES Thread
          3. Initializing EGL
          4. Initializing GL
          5. Drawing on the Screen
        4. Drawing 3D Objects
          1. Drawing Your Vertices
          2. Coloring Your Vertices
          3. Drawing More Complex Objects
          4. Lighting Your Scene
          5. Texturing Your Objects
        5. Interacting with Android Views and Events
          1. Enabling the OpenGL Thread to Talk to the Application Thread
          2. Enabling the Application Thread to Talk to the OpenGL Thread
        6. Cleaning Up OpenGL ES
        7. Using GLSurfaceView (Easy OpenGL ES)
        8. Using OpenGL ES 2.0
          1. Configuring Your Application for OpenGL ES 2.0
          2. Requesting an OpenGL ES 2.0 Surface
        9. Exploring OpenGL ES 3.0
        10. Summary
        11. Quiz Questions
        12. Exercises
        13. References and More Information
      4. 25. Using the Android NDK
        1. Determining When to Use the Android NDK
        2. Installing the Android NDK
        3. Exploring the Android NDK Sample Application
        4. Creating Your Own NDK Project
          1. Calling Native Code from Java
          2. Handling Parameters and Return Values
          3. Using Exceptions with Native Code
          4. Using Native Activities
        5. Improving Graphics Performance
        6. Comparing RenderScript to the NDK
          1. Computing with RenderScript
          2. Native RenderScript
        7. Summary
        8. Quiz Questions
        9. Exercises
        10. References and More Information
    16. VI: Maximizing Android’s Unique Features
      1. 26. Extending Android Application Reach
        1. Enhancing Your Applications
        2. Working with App Widgets
          1. Creating an App Widget
          2. Installing an App Widget to the Home Screen
          3. Becoming an App Widget Host
          4. Introducing Lock Screen App Widgets
          5. Installing an App Widget to the Lock Screen
        3. Working with Live Wallpapers
        4. Creating a Live Wallpaper
          1. Creating a Live Wallpaper Service
          2. Creating a Live Wallpaper Configuration
          3. Configuring the Android Manifest File for Live Wallpapers
          4. Installing a Live Wallpaper
        5. Introducing Daydream
        6. Acting as a Content Type Handler
        7. Determining Intent Actions and MIME Types
          1. Implementing the Activity to Process the Intents
          2. Registering the Intent Filter
        8. Summary
        9. Quiz Questions
        10. Exercises
        11. References and More Information
      2. 27. Enabling Application Search
        1. Making Application Content Searchable
        2. Enabling Searches in Your Application
          1. Creating a Search Configuration
          2. Creating a Search Activity
          3. Configuring the Android Manifest File for Search
        3. Enabling Global Search
          1. Updating a Search Configuration for Global Searches
          2. Updating Search Settings for Global Searches
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
      3. 28. Managing User Accounts and Synchronizing User Data
        1. Managing Accounts with the Account Manager
          1. Multiple Users, Restricted Profiles, and Accounts
        2. Synchronizing Data with Sync Adapters
        3. Using Backup Services
          1. Choosing a Remote Backup Service
          2. Implementing a Backup Agent
          3. Backing Up and Restoring Application Data
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
    17. VII: Advanced Topics in Application Publication and Distribution
      1. 29. Internationalizing Your Applications
        1. Localizing Your Application’s Language
          1. Internationalization Using Alternative Resources
          2. Changing the Language Settings
          3. Implementing Locale Support Programmatically
        2. Right-to-Left Language Localization
        3. Translation Services through Google Play
          1. Using the Developer Console
        4. Publishing Applications for Foreign Users
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      2. 30. Protecting Applications from Software Piracy
        1. All Applications Are Vulnerable
        2. Using Secure Coding Practices
        3. Obfuscating with ProGuard
          1. Configuring ProGuard for Your Android Applications
          2. Dealing with Error Reports after Obfuscation
        4. Leveraging the License Verification Library
        5. Other Antipiracy Tips
        6. Summary
        7. Quiz Questions
        8. Exercises
        9. References and More Information
    18. VIII: Preparing for Future Android Releases
      1. 31. Introducing the L Developer Preview
        1. Exploring the L Developer Preview
          1. Improving Performance
          2. Improving the User Experience
        2. Introducing Android TV
          1. Understanding Android TV Development Requirements
          2. Understanding TV Application Hardware Limitations
        3. Summary
        4. Quiz Questions
        5. Exercises
        6. References and More Information
    19. IX: Appendixes
      1. A. Quick-Start Guide: Android Debug Bridge
        1. Listing Connected Devices and Emulators
        2. Directing ADB Commands to Specific Devices
        3. Starting and Stopping the ADB Server
          1. Stopping the ADB Server Process
          2. Starting and Checking the ADB Server Process
        4. Listing ADB Commands
        5. Issuing Shell Commands
          1. Issuing a Single Shell Command
          2. Using a Shell Session
          3. Using the Shell to Start and Stop the Emulator
        6. Copying Files
          1. Sending Files to a Device or Emulator
          2. Retrieving Files from a Device or Emulator
        7. Installing and Uninstalling Applications
          1. Installing Applications
          2. Reinstalling Applications
          3. Uninstalling Applications
        8. Working with LogCat Logging
          1. Displaying All Log Information
          2. Including Date and Time with Log Data
          3. Filtering Log Information
          4. Clearing the Log
          5. Redirecting Log Output to a File
          6. Accessing the Secondary Logs
        9. Controlling the Backup Service
          1. Forcing Backup Operations
          2. Forcing Restore Operations
          3. Wiping Archived Data
        10. Generating Bug Reports
        11. Using the Shell to Inspect SQLite Databases
        12. Using the Shell to Stress Test Applications
          1. Letting the Monkey Loose on Your Application
          2. Listening to Your Monkey
          3. Directing Your Monkey’s Actions
          4. Training Your Monkey to Repeat His Tricks
          5. Keeping the Monkey on a Leash
          6. Learning More about Your Monkey
        13. Installing Custom Binaries via the Shell
        14. Summary
        15. Quiz Questions
        16. Exercises
        17. References and More Information
      2. B. Quick-Start Guide: SQLite
        1. Exploring Common Tasks with SQLite
        2. Using the sqlite3 Command-Line Interface
          1. Launching the ADB Shell
          2. Connecting to a SQLite Database
          3. Exploring Your Database
          4. Importing and Exporting the Database and Its Data
          5. Executing SQL Commands on the Command Line
          6. Using Other sqlite3 Commands
          7. Understanding SQLite Limitations
        3. Learning by Example: A Student Grade Database
          1. Designing the Student Grade Database Schema
          2. Creating Simple Tables with AUTOINCREMENT
          3. Inserting Data into Tables
          4. Querying Tables for Results with SELECT
          5. Using Foreign Keys and Composite Primary Keys
          6. Altering and Updating Data in Tables
          7. Querying Multiple Tables Using JOIN
          8. Using Calculated Columns
          9. Using Subqueries for Calculated Columns
          10. Deleting Tables
        4. Summary
        5. Quiz Questions
        6. Exercises
        7. References and More Information
      3. C. Java for Android Developers
        1. Learning the Java Programming Language
        2. Learning the Java Development Tools
        3. Familiarizing Yourself with Java Documentation
        4. Understanding Java Shorthand
          1. Chaining Methods and Unnecessary Temp Variables
          2. Looping Infinitely
          3. Working with Unary and Ternary Operators
          4. Working with Inner Classes
        5. Summary
        6. Quiz Questions
        7. Exercises
        8. References and More Information
      4. D. Quick-Start Guide: Android Studio
        1. Getting Up and Running with Android Studio
          1. Launching Android Studio for the First Time
          2. Configuring Android Studio
          3. Creating an Android Studio Project
        2. Understanding the Android Studio Project Structure
        3. Learning about the Gradle Build System
        4. Overview of the Android Studio User Interface
        5. Introducing the Layout Editor
          1. Working in Design View
          2. Working in Text View
          3. Using the Preview Controls
        6. Debugging Your Android Studio Applications
          1. Setting Breakpoints
          2. Stepping through Code
        7. Useful Keyboard Shortcuts
        8. Summary
        9. Quiz Questions
        10. Exercises
        11. References and More Information
      5. E. Answers to Quiz Questions
        1. Chapter 1
        2. Chapter 2
        3. Chapter 3
        4. Chapter 4
        5. Chapter 5
        6. Chapter 6
        7. Chapter 7
        8. Chapter 8
        9. Chapter 9
        10. Chapter 10
        11. Chapter 11
        12. Chapter 12
        13. Chapter 13
        14. Chapter 14
        15. Chapter 15
        16. Chapter 16
        17. Chapter 17
        18. Chapter 18
        19. Chapter 19
        20. Chapter 20
        21. Chapter 21
        22. Chapter 22
        23. Chapter 23
        24. Chapter 24
        25. Chapter 25
        26. Chapter 26
        27. Chapter 27
        28. Chapter 28
        29. Chapter 29
        30. Chapter 30
        31. Chapter 31
        32. Appendix A
        33. Appendix B
        34. Appendix C
        35. Appendix D
    20. Index
    21. Code Snippets