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

Book Description

What does it take to build well-engineered Android applications? Explore Android's core building blocks and APIs in depth with this authoritative guide, and learn how to create compelling apps that work on a full range of Android devices. You'll work with proven approaches to app design and implementation—including application frameworks that you can use as a starting point for your own projects.

Delve into sensors, native development, 3D graphics, and many other topics, and discover how to build apps on the platform of your choice. If you're an intermediate to advanced programmer, you'll learn how to make great Android apps.

  • Learn how to use the Android SDK with the Eclipse IDE
  • Apply advanced Java concepts regardless of your experience with the language
  • Create an Android user interface that's captivating and easy to navigate
  • Use the Fragment API for tablet user interfaces
  • Make your application compatible with Honeycomb and earlier versions
  • Understand Android's unique database design issues and the role of SQLite
  • Use sensors and gestures to expand your app's input beyond just tapping and scrolling
  • Explore Android APIs for multimedia, location, communication, NFC, and other applications

Table of Contents

  1. A Note Regarding Supplemental Files
  2. 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
  3. I. Tools and Basics
    1. 1. Your Toolkit
      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 Development Toolkit (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. Draw9patch
          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. Static Application Resources and Context
        1. Application Manifests
        2. A Typical Source Tree
        3. Initialization Parameters in AndroidManifest.xml
      5. Resources
      6. The Android Application Runtime Environment
        1. The Dalvik VM
        2. Zygote: Forking a New Process
        3. Sandboxing: Processes and Users
      7. Component Life Cycles
        1. The Activity Life Cycle
      8. Packaging an Android Application: The .apk File
      9. On Porting Software to Android
    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. Google Maps API Keys
      4. Specifying API-Level Compatibility
      5. 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
    6. 6. Effective Java for Android
      1. The Android Framework
        1. The Android Libraries
        2. Extending Android
          1. Overrides and callbacks
          2. Using polymorphism and composition
          3. Extending Android classes
      2. Organizing Java Source
      3. Concurrency in Android
        1. AsyncTask and the UI Thread
        2. Threads in an Android Process
      4. Serialization
        1. Java Serialization
        2. Parcelable
        3. Classes That Support Serialization
        4. Serialization and the Application Life Cycle
  4. II. About the Android Framework
    1. 7. 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. Listening for Key Events
        4. Alternative Ways to Handle Events
        5. Advanced Wiring: Focus and Threading
      4. The Menu
    2. 8. Fragments and Multiplatform Support
      1. Creating a Fragment
      2. Fragment Life Cycle
      3. The Fragment Manager
      4. Fragment Transactions
      5. The Compatibility Package
    3. 9. 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, and Filters
        2. Animation
          1. Transition animation
          2. Background animation
          3. Surface view animation
        3. OpenGL Graphics
    4. 10. 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
  5. III. A Skeleton Application for Android
    1. 11. 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
      5. A Flowing and Intuitive User Experience Across Activities
        1. Multitasking in a Small-Screen Environment
        2. Tasks and Applications
        3. Specifying Launch and Task Behavior
          1. Launch mode
          2. Task affinity
          3. Other activity attributes affecting task behavior
          4. Modifying task behavior with intent flags
    2. 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
    3. 13. Exploring Content Providers
      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
  6. IV. Advanced Topics
    1. 14. 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
    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
    3. 16. 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
      3. Gesture Input
      4. Accessibility
    4. 17. 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
    5. 18. The Android Native Development Kit (NDK)
      1. Native Methods and JNI Calls
        1. Conventions on the Native Method Side
        2. Conventions on the Java Side
      2. The Android NDK
        1. Setting Up the NDK Environment
        2. Compiling with the NDK
        3. JNI, NDK, and SDK: A Sample App
      3. Android-Provided Native Libraries
      4. Building Your Own Custom Library Modules
      5. Native Activities
  7. Index
  8. About the Authors
  9. Colophon
  10. Copyright