O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programming Android, 2nd Edition

Book Description

Get thoroughly up to speed on Android programming, and learn how to create up-to-date user experiences for both handsets and tablets. With this book's extensively revised second edition, you'll focus on Android tools and programming essentials, including best practices for using Android 4 APIs. If you're experienced with Java or Objective-C, you'll gain the knowledge necessary for building well-engineered applications.

Programming Android is organized into four parts:

  • Part One helps programmers with some Java or iOS experience get off to a fast start with the Android SDK and Android programming basics.
  • Part Two delves into the Android framework, focusing on user interface and graphics class hierarchies, concurrency, and databases. It's a solid foundation for understanding of how the most important parts of an Android application work.
  • Part Three features code skeletons and patterns for accelerating the development of apps that use web data and Android 4 user interface conventions and APIs.
  • Part Four delivers practical coverage of Android's multimedia, search, location, sensor, and account APIs, plus the Native Development Kit, enabling developers to add advanced capabilities.

This updated edition of Programming Android focuses on the knowledge and developer priorities that are essential for successful Android development projects.

Table of Contents

  1. Preface
    1. How This Book Is Organized
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Safari® Books Online
    5. How to Contact Us
    6. Acknowledgments
  2. I. Tools and Basics
    1. 1. Installing the Android SDK and Prerequisites
      1. Installing the Android SDK and Prerequisites
        1. The Java Development Kit (JDK)
        2. The Eclipse Integrated Development Environment (IDE)
        3. The Android SDK
        4. Adding Build Targets to the SDK
        5. The Android Developer Tools (ADT) Plug-in for Eclipse
          1. Using the Install New Software Wizard to download and install the ADT plug-in
          2. Configuring the ADT plug-in
      2. Test Drive: Confirm That Your Installation Works
        1. Making an Android Project
        2. Making an Android Virtual Device (AVD)
        3. Running a Program on an AVD
        4. Running a Program on an Android Device
        5. Troubleshooting SDK Problems: No Build Targets
      3. Components of the SDK
        1. The Android Debug Bridge (adb)
        2. The Dalvik Debug Monitor Server (DDMS)
        3. Components of the ADT Eclipse Plug-in
          1. The Android Layout Editor
          2. The Android Manifest Editor
          3. XML editors for other Android XML files
          4. Building Android apps
          5. Running and debugging Android apps
          6. The DDMS
        4. Android Virtual Devices
          1. QEMU
          2. The SDK and AVD Manager
        5. Other SDK Tools
          1. Hierarchy Viewer
          2. Layoutopt
          3. Monkey
          4. sqlite3
          5. keytool
          6. Zipalign
          7. Draw 9-patch
          8. android
      4. Keeping Up-to-Date
        1. Keeping the Android SDK Up-to-Date
        2. Keeping Eclipse and the ADT Plug-in Up-to-Date
        3. Keeping the JDK Up-to-Date
      5. Example Code
        1. SDK Example Code
        2. Example Code from This Book
      6. On Reading Code
    2. 2. Java for Android
      1. Android Is Reshaping Client-Side Java
      2. The Java Type System
        1. Primitive Types
        2. Objects and Classes
        3. Object Creation
        4. The Object Class and Its Methods
        5. Objects, Inheritance, and Polymorphism
        6. Final and Static Declarations
        7. Abstract Classes
        8. Interfaces
        9. Exceptions
        10. The Java Collections Framework
          1. Collection interface types
          2. Collection implementation types
          3. Java generics
        11. Garbage Collection
      3. Scope
        1. Java Packages
        2. Access Modifiers and Encapsulation
      4. Idioms of Java Programming
        1. Type Safety in Java
          1. Encapsulation
          2. Getters and setters
        2. Using Anonymous Classes
        3. Modular Programming in Java
        4. Basic Multithreaded Concurrent Programming in Java
        5. Synchronization and Thread Safety
        6. Thread Control with wait() and notify() Methods
        7. Synchronization and Data Structures
    3. 3. The Ingredients of an Android Application
      1. Traditional Programming Models Compared to Android
      2. Activities, Intents, and Tasks
      3. Other Android Components
        1. Service
        2. Content Providers
          1. Using a content provider
          2. Content providers and the Internet
        3. BroadcastReceiver
      4. Component Life Cycles
        1. The Activity Life Cycle
        2. On Porting Software to Android
      5. Static Application Resources and Context
        1. Organizing Java Source
        2. Resources
        3. Application Manifests
        4. Initialization Parameters in AndroidManifest.xml
        5. Packaging an Android Application: The .apk File
      6. The Android Application Runtime Environment
        1. The Dalvik VM
        2. Zygote: Forking a New Process
        3. Sandboxing: Processes and Users
        4. The Android Libraries
      7. Extending Android
        1. The Android Application Template
        2. Overrides and Callbacks
        3. Polymorphism and Composition
        4. Extending Android Classes
      8. Concurrency in Android
        1. AsyncTask and the UI Thread
        2. Threads in an Android Process
      9. Serialization
        1. Java Serialization
        2. Parcelable
        3. Classes That Support Serialization
        4. Serialization and the Application Life Cycle
    4. 4. Getting Your Application into Users’ Hands
      1. Application Signing
        1. Public Key Encryption and Cryptographic Signing
        2. How Signatures Protect Software Users, Publishers, and Secure Communications
          1. Self-signed certificates for Android software
        3. Signing an Application
          1. Debug certificates
          2. Creating a self-signed certificate
          3. Don’t lose it!
          4. Using a self-signed certificate to sign an application
      2. Placing an Application for Distribution in the Android Market
        1. Becoming an Official Android Developer
        2. Uploading Applications in the Market
        3. Getting Paid
      3. Alternative Distribution
        1. Verizon Applications for Android
          1. How to submit to the Verizon Apps store for Android
          2. Technical tips for Verizon Android development
          3. Related links
        2. Amazon Applications for Android
      4. Google Maps API Keys
      5. Specifying API-Level Compatibility
      6. Compatibility with Many Kinds of Screens
        1. Testing for Screen Size Compatibility
        2. Resource Qualifiers and Screen Sizes
    5. 5. Eclipse for Android Software Development
      1. Eclipse Concepts and Terminology
        1. Plug-ins
        2. Workspaces
        3. Java Environments
          1. Eclipse’s Java Runtime Environment
          2. The Java compiler
          3. The application runtime
        4. Projects
        5. Builders and Artifacts
        6. Extensions
        7. Associations
      2. Eclipse Views and Perspectives
        1. The Package Explorer View
        2. The Task List View
        3. The Outline View
        4. The Problems View
      3. Java Coding in Eclipse
        1. Editing Java Code and Code Completion
        2. Refactoring
      4. Eclipse and Android
      5. Preventing Bugs and Keeping Your Code Clean
        1. Static Analyzers
          1. FindBugs
        2. Applying Static Analysis to Android Code
        3. Limitations of Static Analysis
      6. Eclipse Idiosyncrasies and Alternatives
  3. II. About the Android Framework
    1. 6. Building a View
      1. Android GUI Architecture
        1. The Model
        2. The View
        3. The Controller
        4. Putting It Together
      2. Assembling a Graphical Interface
      3. Wiring Up the Controller
        1. Listening to the Model
        2. Listening for Touch Events
        3. Multiple Pointers and Gestures
        4. Listening for Key Events
        5. Choosing an Event Handler
        6. Advanced Wiring: Focus and Threading
      4. The Menu and the Action Bar
      5. View Debugging and Optimization
    2. 7. Fragments and Multiplatform Support
      1. Creating a Fragment
      2. Fragment Life Cycle
      3. The Fragment Manager
      4. Fragment Transactions
      5. The Support Package
      6. Fragments and Layout
    3. 8. Drawing 2D and 3D Graphics
      1. Rolling Your Own Widgets
        1. Layout
          1. Measurement
          2. Arrangement
        2. Canvas Drawing
          1. Drawing text
          2. Matrix transformations
        3. Drawables
        4. Bitmaps
      2. Bling
        1. Shadows, Gradients, Filters, and Hardware Acceleration
        2. Animation
          1. Transition animation
          2. Background animation
          3. Surface view animation
        3. OpenGL Graphics
    4. 9. Handling and Persisting Data
      1. Relational Database Overview
      2. SQLite
      3. The SQL Language
        1. SQL Data Definition Commands
          1. SQLite types
          2. Database constraints
        2. SQL Data Manipulation Commands
        3. Additional Database Concepts
        4. Database Transactions
        5. Example Database Manipulation Using sqlite3
      4. SQL and the Database-Centric Data Model for Android Applications
      5. The Android Database Classes
      6. Database Design for Android Applications
        1. Basic Structure of the SimpleVideoDbHelper Class
      7. Using the Database API: MJAndroid
        1. Android and Social Networking
        2. The Source Folder (src)
        3. Loading and Starting the Application
        4. Database Queries and Reading Data from the Database
          1. Using the query method
        5. Modifying the Database
          1. Inserting data into the database
            1. Using the insert method
            2. Using the execSQL method
          2. Updating data already in the database
            1. Using the update method
            2. Using the execSQL method
          3. Deleting data in the database
            1. Using the delete method
            2. Using the execSQL method
  4. III. A Skeleton Application for Android
    1. 10. A Framework for a Well-Behaved Application
      1. Visualizing Life Cycles
        1. Visualizing the Activity Life Cycle
          1. Memory recovery and life cycles
          2. Life cycle methods of the Activity class
          3. Saving and restoring instance state
          4. Configuration changes and the activity life cycle
          5. Minor life cycle methods of the Activity class
      2. Visualizing the Fragment Life Cycle
      3. The Activity Class and Well-Behaved Applications
        1. The Activity Life Cycle and the User Experience
      4. Life Cycle Methods of the Application Class
    2. 11. Building a User Interface
      1. Top-Level Design
        1. Fragment, Activity, and Scalable Design
      2. Visual Editing of User Interfaces
      3. Starting with a Blank Slate
      4. Laying Out the Fragments
        1. Lay Out Fragments Using the Visual Editor
        2. Multiple Layouts
      5. Folding and Unfolding a Scalable UI
        1. Decisions about Screen Size and Resolution
        2. Delegating to Fragment Classes
      6. Making Activity, Fragment, Action Bar, and Multiple Layouts Work Together
        1. Action Bar
        2. Tabs and Fragments
      7. The Other Activity
    3. 12. Using Content Providers
      1. Understanding Content Providers
        1. Implementing a Content Provider
        2. Browsing Video with Finch
          1. The simple video database
          2. Structure of the simple version of the code
      2. Defining a Provider Public API
        1. Defining the CONTENT_URI
        2. Creating the Column Names
        3. Declaring Column Specification Strings
      3. Writing and Integrating a Content Provider
        1. Common Content Provider Tasks
          1. Extending ContentProvider
      4. File Management and Binary Data
      5. Android MVC and Content Observation
      6. A Complete Content Provider: The SimpleFinchVideoContentProvider Code
        1. The SimpleFinchVideoContentProvider Class and Instance Variables
        2. Implementing the onCreate Method
        3. Implementing the getType Method
        4. Implementing the Provider API
          1. The query method
          2. The insert method
          3. The update method
          4. The delete method
        5. Determining How Often to Notify Observers
      7. Declaring Your Content Provider
    4. 13. A Content Provider as a Facade for a RESTful Web Service
      1. Developing RESTful Android Applications
      2. A “Network MVC”
      3. Summary of Benefits
      4. Code Example: Dynamically Listing and Caching YouTube Video Content
      5. Structure of the Source Code for the Finch YouTube Video Example
      6. Stepping Through the Search Application
      7. Step 1: Our UI Collects User Input
      8. Step 2: Our Controller Listens for Events
      9. Step 3: The Controller Queries the Content Provider with a managedQuery on the Content Provider/Model
      10. Step 4: Implementing the RESTful Request
        1. Constants and Initialization
        2. Creating the Database
        3. A Networked Query Method
          1. RESTfulContentProvider: A REST helper
            1. UriRequestTask
            2. YouTubeHandler
        4. insert and ResponseHandlers
        5. File Management: Storing Thumbnails
  5. IV. Advanced Topics
    1. 14. Search
      1. Search Interface
        1. Search Basics
          1. Search logic
            1. Index-based search logic
            2. Database-backed search logic
          2. Searchable configuration
          3. Searchable activity
        2. Search Dialog
        3. Search Widget
      2. Query Suggestions
        1. Recent Query Suggestions
        2. Custom Query Suggestions
    2. 15. Location and Mapping
      1. Location-Based Services
      2. Mapping
      3. The Google Maps Activity
      4. The MapView and MapActivity
      5. Working with MapViews
      6. MapView and MyLocationOverlay Initialization
      7. Pausing and Resuming a MapActivity
      8. Controlling the Map with Menu Buttons
      9. Controlling the Map with the Keypad
      10. Location Without Maps
        1. The Manifest and Layout Files
        2. Connecting to a Location Provider and Getting Location Updates
        3. Updating the Emulated Location
          1. Using geo to update location
          2. Using DDMS to update location
      11. StreetView
    3. 16. Multimedia
      1. Audio and Video
      2. Playing Audio and Video
        1. Audio Playback
          1. MediaPlayer audio playback
          2. AudioTrack audio playback
        2. Video Playback
      3. Recording Audio and Video
        1. Audio Recording
          1. MediaRecorder audio recording
          2. Intent audio recording
          3. AudioRecorder audio recording
        2. Video Recording
          1. MediaRecorder video recording
          2. Intent video recording
      4. Stored Media Content
    4. 17. Sensors, NFC, Speech, Gestures, and Accessibility
      1. Sensors
        1. Position
          1. Accelerometer
          2. Gyroscope
          3. Rotation vector
          4. Linear acceleration
          5. Gravity
        2. Other Sensors
      2. Near Field Communication (NFC)
        1. Reading a Tag
        2. Writing to a Tag
        3. P2P Mode and Beam
          1. API levels 10−13
          2. Beam: API level 14+
      3. Gesture Input
      4. Accessibility
    5. 18. Communication, Identity, Sync, and Social Media
      1. Account Contacts
      2. Authentication and Synchronization
        1. Authentication
        2. Synchronization
      3. Bluetooth
        1. The Bluetooth Protocol Stack
          1. Bluetooth-specific protocols and adopted protocols
        2. BlueZ: The Linux Bluetooth Implementation
        3. Using Bluetooth in Android Applications
          1. Bluetooth and related I/O classes
          2. The DeviceListActivity class
          3. The BtConsoleActivity class
    6. 19. The Android Native Development Kit (NDK)
      1. Native Methods and JNI Calls
        1. Conventions in Native Method Calls
        2. Conventions on the Java Side
      2. The Android NDK
        1. Setting Up the NDK Environment
        2. Editing C/C++ Code in Eclipse
        3. Compiling with the NDK
        4. JNI, NDK, and SDK: A Sample App
      3. Native Libraries and Headers Provided by the NDK
      4. Building Your Own Custom Library Modules
      5. Native Activities
  6. Index
  7. About the Authors
  8. Colophon
  9. Copyright