You are previewing Pro Android 5, Fifth Edition.
O'Reilly logo
Pro Android 5, Fifth Edition

Book Description

Pro Android 5 shows you how to build real-world and fun mobile apps using the Android 5 SDK. This book updates the best-selling Pro Android and covers everything from the fundamentals of building apps for smartphones, tablets, and embedded devices to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more.

  • Using the tutorials and expert advice, you'll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones.
  • You'll explore and use the Android APIs, including those for media and sensors.
  • And you'll check out what's new in Android, including the improved user interface across all Android platforms, integration with services, and more.
  • By reading this definitive tutorial and reference, you'll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future.

    Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Authors
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Foreword
    11. Introduction
    12. Chapter 1: Hello Android
      1. Prerequisites for Android Development
      2. Setting Up Your Eclipse Environment
        1. Downloading JDK 6
        2. Downloading Eclipse 4.2
        3. Downloading the Android SDK
        4. The Tools Window
        5. Installing ADT
      3. Setting Up Your Android Studio Environment
        1. Java requirements for Android Studio
        2. Downloading and Installing Android Studio
      4. Learning Android’s Fundamental Components
        1. View
        2. Activity
        3. Fragment
        4. Intent
        5. Content Provider
        6. Service
        7. AndroidManifest.xml
        8. AVDs
      5. Hello World!
      6. AVDs
      7. Running on a Real Device
      8. Exploring the Structure of an Android Application
      9. Examining the Application Life Cycle
      10. Simple Debugging
        1. Launching the Emulator
      11. References
      12. Summary
    13. Chapter 2: Introduction to Android Application Architecture
      1. Exploring a Simple Android Application
      2. Defining UI through Layout Files
        1. Specifying Comments in Layout Files
        2. Adding Views and View Groups in Layout Files
        3. Specifying Control Properties in Layout Files
        4. Indicating ViewGroup Properties
        5. Controlling Width and Height of a Control
        6. Introducing Resources and Backgrounds
        7. Working with Text Controls in the Layout File
        8. Working with Autogenerated IDs for Controls
      3. Implementing Programming Logic
        1. Loading the Layout File into an Activity
        2. Gathering Controls
        3. Setting Up Buttons
        4. Responding to Button Clicks: Tying It All Together
      4. Updating the AndroidManifest.XML
      5. Placing the Files in the Android Project
      6. Testing the Calculator App on a Real Device
      7. Android Activity Life Cycle
        1. void onCreate(Bundle savedInstanceState)
        2. void onStart()
        3. void onRestoreInstanceState(Bundle savedInstanceState)
        4. void onResume()
        5. void onPause()
        6. void onStop()
        7. void onSaveInstanceState(Bundle saveStateBundle)
        8. void onRestart()
        9. Object onRetainNonConfigurationInstance()
        10. void onDestroy()
        11. General Notes on Activity Callbacks
      8. More on Resources
        1. Directory Structure of Resources
        2. Reading Resources from Java Code
        3. Runtime Behavior of Drawable Resources
        4. Using Arbitrary XML Files as Resources
        5. Working with Raw Resource Files
        6. Reading Files from the Assets Directory
        7. Reading Resources and Assets Without an Activity Reference
        8. Understanding Resource Directories, Language, and Locale
      9. More on Intents
        1. Starting Activities for Results
        2. Exercising the GET_CONTENT Action
        3. Relating Intents and Activities
        4. Understanding Explicit and Implicit Intents
        5. Saving State in Android
      10. Roadmap for Learning Android and the Rest of the Book
        1. Track 1: UI Essentials for Your Android Applications
        2. Track 2: Saving State
        3. Track 3: Preparing/Taking Your Application to the Market
        4. Track 4: Making Your Application Robust
        5. Track 5: Bringing Finesse to Your Apps
        6. Track 6: Integrating with Other Devices and the Cloud
        7. Final Track: Getting a Helping Hand from Expert Android
      11. As We Leave You Now with the Rest of the Book
      12. References
      13. Summary
    14. Chapter 3: Building Basic User Interfaces and Using Controls
      1. UI Development in Android
        1. Building a UI Completely in Code
        2. Building a UI Completely in XML
        3. Building a UI in XML with Code
      2. Understanding Android’s Common Controls
        1. Text Controls
        2. Button Controls
        3. The ImageView Control
        4. Date and Time Controls
        5. The MapView Control
      3. References
      4. Summary
    15. Chapter 4: Adapters and List Controls
      1. Understanding Adapters
        1. Getting to Know SimpleCursorAdapter
        2. Getting to Know ArrayAdapter
      2. Using Adapters with AdapterViews
        1. The Basic List Control: ListView
        2. The GridView Control
        3. The Spinner Control
        4. The Gallery Control
      3. Summary
    16. Chapter 5: Building More Advanced UI Layouts
      1. Creating Custom Adapters
        1. Other Controls in Android
      2. Styles and Themes
        1. Using Styles
        2. Using Themes
      3. Understanding Layout Managers
        1. The LinearLayout Layout Manager
        2. The TableLayout Layout Manager
        3. The RelativeLayout Layout Manager
        4. The FrameLayout Layout Manager
        5. The GridLayout Layout Manager
        6. Customizing the Layout for Various Device Configurations
      4. Summary
    17. Chapter 6: Working with Menus and Action Bars
      1. Working with Menus Through XML Files
        1. Creating XML Menu Resource Files
        2. Populating Activity Menu from Menu XML Files
        3. Responding to XML-Based Menu Items
      2. Working with Menus in Java Code
        1. Working with Menu Groups
      3. Understanding Expanded Menus
      4. Working with Icon Menus
      5. Working with Submenus
      6. Working with Context Menus
        1. Registering a View for a Context Menu
        2. Populating a Context Menu
        3. Responding to Context Menu Items
      7. Incorporating Dynamic Menus
        1. Working with Pop-up Menus
      8. Exploring Action Bars
      9. Implementing a Standard Action Bar
      10. Implementing a Tabbed Action Bar
      11. Implementing a List-Based Action Bar
      12. Exploring Action Bar and Search View
        1. Defining a Search View Widget as a Menu Item
        2. Creating a Search Results Activity
        3. Specifying a Searchable XML File
        4. Defining the Search Results Activity in the Manifest File
        5. Identifying the Search Target for the Search View Widget
      13. Resources
      14. Summary
    18. Chapter 7: Styles and Themes
      1. Using Styles
      2. Using Themes
      3. References
      4. Summary
    19. Chapter 8: Fragments
      1. What Is a Fragment?
        1. When to Use Fragments
        2. The Structure of a Fragment
        3. A Fragment’s Life Cycle
        4. Sample Fragment App Showing the Life Cycle
      2. FragmentTransactions and the Fragment Back Stack
        1. Fragment Transaction Transitions and Animations
      3. The FragmentManager
        1. Caution When Referencing Fragments
        2. Saving Fragment State
        3. ListFragments and <fragment>
        4. Invoking a Separate Activity When Needed
        5. Persistence of Fragments
      4. Communications with Fragments
        1. Using startActivity() and setTargetFragment()
      5. References
      6. Summary
    20. Chapter 9: Responding to Configuration Changes
      1. The Default Configuration Change Process
        1. The Destroy/Create Cycle of Activities
        2. The Destroy/Create Cycle of Fragments
        3. Using FragmentManager to Save Fragment State
        4. Using setRetainInstance on a Fragment
      2. Deprecated Configuration Change Methods
      3. Handling Configuration Changes Yourself
      4. References
      5. Summary
    21. Chapter 10: Working with Dialogs
      1. Using Dialogs in Android
      2. Understanding Dialog Fragments
        1. DialogFragment Basics
        2. DialogFragment Sample Application
      3. Working with Toast
      4. References
      5. Summary
    22. Chapter 11: Working with Preferences and Saving State
      1. Exploring the Preferences Framework
        1. Understanding CheckBoxPreference and SwitchPreference
        2. Accessing a Preference Value in Code
        3. Understanding ListPreference
        4. Understanding EditTextPreference
        5. Understanding MultiSelectListPreference
        6. Updating AndroidManifest.xml
        7. Using PreferenceCategory
        8. Creating Child Preferences with Dependency
        9. Preferences with Headers
        10. PreferenceScreens
      2. Dynamic Preference Summary Text
        1. Saving State with Preferences
        2. Using DialogPreference
      3. Reference
      4. Summary
    23. Chapter 12: Using the Compatibility Library for Older Devices
      1. It All Started with Tablets
      2. Adding the Library to Your Project
        1. Including the v7 Support Library
        2. Including the v8 Support Library
        3. Including the v13 Support Library
        4. Including the v17 Support Library
        5. Including Just the v4 Support Library
      3. Retrofitting an App with the Android Support Library
      4. References
      5. Summary
    24. Chapter 13: Exploring Packages, Processes, Threads, and Handlers
      1. Understanding Packages and Processes
      2. A Code Pattern for Sharing Data
      3. Understanding Library Projects
      4. Understanding Components and Threads
      5. Understanding Handlers
      6. Using Worker Threads
      7. References
      8. Summary
    25. Chapter 14: Building and Consuming Services
      1. Consuming HTTP Services
        1. Using the HttpClient for HTTP GET Requests
        2. Using the HttpClient for HTTP POST Requests (a Multipart Example)
        3. SOAP, JSON, and XML Parsers
        4. Dealing with Exceptions
        5. Addressing Multithreading Issues
        6. Fun with Timeouts
        7. Using the HttpURLConnection
        8. Using the AndroidHttpClient
      2. Using Android Services
        1. Understanding Services in Android
        2. Understanding Local Services
        3. Understanding AIDL Services
        4. Defining a Service Interface in AIDL
        5. Implementing an AIDL Interface
        6. Calling the Service from a Client Application
        7. Passing Complex Types to Services
        8. Messengers and Handlers
      3. References
      4. Summary
    26. Chapter 15: Advanced AsyncTask and Progress Dialogs
      1. Essentials of a Simple AsyncTask
        1. Implementing Your First AsyncTask
        2. Calling an AsyncTask
        3. Understanding the onPreExecute() Callback and Progress Dialog
        4. Understanding the doInBackground() Method
        5. Triggering onProgressUpdate() through publishProgress()
        6. Understanding the onPostExecute() Method
        7. Upgrading to a Deterministic Progress Dialog
      2. AsyncTask and Thread Pools
      3. Issues and Solutions for Correctly Showing the Progress of an AsyncTask
        1. Dealing with Activity Pointers and Device Rotation
        2. Dealing with Managed Dialogs
        3. Using Retained Objects and Fragment Dialogs
        4. Using Retained Fragments and Fragment Dialogs
        5. Using Retained Fragments and ProgressBars
      4. References
      5. Summary
    27. Chapter 16: Broadcast Receivers and Long-Running Services
      1. Sending a Broadcast
        1. Coding a Simple Receiver
        2. Registering a Receiver in the Manifest File
        3. Accommodating Multiple Receivers
        4. Working with Out-of-Process Receivers
      2. Using Notifications from a Receiver
        1. Monitoring Notifications Through the Notification Manager
        2. Sending a Notification
        3. Starting an Activity in a Broadcast Receiver
      3. Exploring Long-Running Receivers and Services
        1. Understanding Long-Running Broadcast Receiver Protocol
        2. Understanding IntentService
      4. Extending IntentService for a Broadcast Receiver
        1. Exploring Long-Running Broadcast Service Abstraction
        2. Designing A Long-Running Receiver
        3. Abstracting a Wake Lock with LightedGreenRoom
      5. Implementing a Long-Running Service
        1. Understanding a Nonsticky Service
        2. Understanding a Sticky Service
        3. Understanding Redeliver Intents Option
        4. Coding a Long-Running Service
      6. Additional Topics in Broadcast Receivers
      7. References
      8. Summary
    28. Chapter 17: Exploring the Alarm Manager
      1. Setting Up a Simple Alarm
        1. Setting Off an Alarm Repeatedly
      2. Cancelling an Alarm
      3. Understanding Exactness of Alarms
      4. Understanding Persistence of Alarms
      5. References
      6. Summary
    29. Chapter 18: Exploring 2D Animation
      1. Exploring Frame-by-Frame Animation
      2. Exploring Layout Animation
        1. Understanding Interpolators
      3. Exploring View Animation
        1. Using Camera to Provide Depth Perception in 2D
        2. Exploring the AnimationListener Class
        3. Notes on Transformation Matrices
      4. Exploring Property Animations: The New Animation API
        1. Understanding Property Animation
        2. Planning a Test Bed for Property Animation
        3. Animating Views with Object Animators
        4. Achieving Sequential Animation with AnimatorSet
        5. Setting Animation Relationships with AnimatorSet.Builder
        6. Using XML to Load Animators
        7. Using PropertyValuesHolder
        8. Understanding View Properties Animation
        9. Understanding Type Evaluators
        10. Understanding Key Frames
        11. Understanding Layout Transitions
      5. Resources
      6. Summary
    30. Chapter 19: Exploring Maps and Location-Based Services
      1. Understanding the Mapping Package
        1. Obtaining a Maps API Key from Google
        2. Adding the Maps API Key to Your Application
      2. Understanding MapFragment
        1. Adding Markers to Maps
        2. Understanding the Location Package
        3. Geocoding with Android
        4. Understanding Location Services
        5. Using Proximity Alerts and Geofencing
      3. References
      4. Summary
    31. Chapter 20: Understanding the Media Frameworks
      1. Using the Media APIs
        1. Whither SD Cards?
      2. Playing Media
        1. Playing Audio Content
        2. Playing Video Content
      3. Bonus Online Chapter on Recording and Advanced Media
        1. References
      4. Summary
    32. Chapter 21: Home Screen Widgets
      1. User Experience with Home Screen Widgets
      2. Understanding Widget Configuration Activity
      3. Understanding the Life Cycle of a Widget
        1. Understanding Widget Definition Phase
      4. Implementing A Sample Widget Application
        1. Defining the Widget Provider
        2. Implementing Widget Configuration Activity
        3. Implementing a Widget Provider
      5. Collection-Based Widgets
      6. Resources
      7. Summary
    33. Chapter 22: Touch Screens
      1. Understanding MotionEvents
        1. The MotionEvent Object
        2. Recycling MotionEvents
        3. Using VelocityTracker
      2. Multitouch
        1. The Basics of Multitouch
      3. Gestures
        1. The Pinch Gesture
        2. GestureDetector and OnGestureListeners
      4. References
      5. Summary
    34. Chapter 23: Implementing Drag and Drop
      1. Exploring Drag and Drop
      2. Basics of Drag and Drop in 3.0+
      3. Drag-and-Drop Example Application
        1. List of Files
        2. Laying Out the Example Drag-and-Drop Application
        3. Responding to onDrag in the Dropzone
        4. Setting Up the Drag Source Views
      4. Testing the Example Drag-and-Drop Application
      5. References
      6. Summary
    35. Chapter 24: Using Sensors
      1. What Is a Sensor?
        1. Detecting Sensors
        2. What Can We Know About a Sensor?
      2. Getting Sensor Events
        1. Issues with Getting Sensor Data
      3. Interpreting Sensor Data
        1. Light Sensors
        2. Proximity Sensors
        3. Temperature Sensors
        4. Pressure Sensors
        5. Gyroscope Sensors
        6. Accelerometers
        7. Magnetic Field Sensors
        8. Using Accelerometers and Magnetic Field Sensors Together
        9. Magnetic Declination and GeomagneticField
        10. Gravity Sensors
        11. Linear Acceleration Sensors
        12. Rotation Vector Sensors
      4. References
      5. Summary
    36. Chapter 25: Exploring Android Persistence and Content Providers
      1. Saving State Using Shared Preferences
      2. Saving State Using Internal Files
      3. Saving State Using External Files
      4. Saving State Using SQLite
      5. Saving State Using O/R Mapping Libraries
      6. Saving State Using Content Providers
      7. Saving State Using Network Storage
      8. Storing Data Directly Using SQLite
        1. Summarizing Key SQLite Packages and Classes
        2. Creating an SQLite Database
        3. Defining a Database Through DDLs
        4. Migrating a Database
        5. Inserting Rows
        6. Updating Rows
        7. Deleting Rows
        8. Reading Rows
        9. Applying Transactions
        10. Summarizing SQLite
        11. Doing Transactions Through Dynamic Proxies
        12. Exploring Databases on the Emulator and Available Devices
      9. Exploring Content Providers
        1. Exploring Android’s Built-in Providers
        2. Understanding the Structure of Content Provider URIs
      10. Implementing Content Providers
        1. Planning a Database
        2. Extending ContentProvider
        3. Using UriMatcher to Figure Out the URIs
        4. Using Projection Maps
        5. Fulfilling MIME-Type Contracts
        6. Implementing the Query Method
        7. Implementing the Insert Method
        8. Implementing the Update Method
        9. Implementing the Delete Method
        10. Registering the Provider
      11. Exercising the Book Provider
        1. Adding a Book
        2. Removing a Book
        3. Displaying the List of Books
      12. Resources
      13. Summary
    37. Chapter 26: Understanding Loaders
      1. Understanding the Architecture of Loaders
      2. Listing Basic Loader API Classes
      3. Demonstrating the Loaders
      4. Step 1: Preparing the Activity to Load Data
      5. Step 2: Initializing the Loader
        1. Delving into the Structure of ListActivity
        2. Working with Asynchronous Loading of Data
      6. Step 3: Implementing onCreateLoader()
      7. Step 4: Implementing onLoadFinished()
      8. Step 5: Implementing onLoaderReset()
      9. Using Search with Loaders
      10. Understanding the Order of LoaderManager Callbacks
      11. Writing Custom Loaders
      12. Resources
      13. Summary
    38. Chapter 27: Exploring the Contacts API
      1. Understanding Accounts
        1. Enumerating Accounts
      2. Understanding Contacts
        1. Examining the Contacts SQLite Database
        2. Understanding Raw Contacts
        3. Understanding the Contacts Data Table
        4. Understanding Aggregated Contacts
        5. Exploring view_contacts
        6. Exploring contact_entities_view
      3. Working with the Contacts API
        1. Exploring Accounts
        2. Exploring Aggregated Contacts
        3. Exploring Raw Contacts
        4. Exploring Raw Contact Data
        5. Adding a Contact with Its Details
      4. Controlling Aggregation of Contacts
      5. Understanding Personal Profile
        1. Reading Profile Raw Contacts
        2. Reading Profile Contact Data
        3. Adding Data to the Personal Profile
      6. Role of Sync Adapters
      7. Using Batch Operations to Optimize ContentProvider Updates
        1. Idea of Batching Content Provider Updates
        2. Batching Commits by Yielding
        3. Using Back References
        4. Optimistic Locking
      8. Reusing the Contact Provider UI
      9. Using Group Features
      10. Using Photo Features
      11. References
      12. Summary
    39. Chapter 28: Exploring Security and Permissions
      1. Understanding the Android Security Model
        1. Overview of Security Concepts
        2. Signing Applications for Deployment
      2. Performing Runtime Security Checks
        1. Understanding Security at the Process Boundary
        2. Declaring and Using Permissions
        3. Understanding and Using URI Permissions
      3. References
      4. Summary
    40. Chapter 29: Using Google Cloud Messaging with Android
      1. What Is Google Cloud Messaging?
        1. Understanding the Key Building Blocks of GCM
        2. Preparing to Use GCM in Your Application
        3. Authenticating GCM Communication
      2. Building an Android GCM-Enabled Application
        1. Coding the Client Component for GCM
        2. Coding the Server Component for GCM
      3. Moving Beyond the GCM Introduction
    41. Chapter 30: Deploying Your Application: Google Play Store and Beyond
      1. Becoming a Publisher
        1. Following the Rules
        2. Developer Console
      2. Preparing Your Application for Sale
        1. Testing for Different Devices
        2. Supporting Different Screen Sizes
        3. Preparing AndroidManifest.xml for Uploading
        4. Localizing Your Application
        5. Preparing Your Application Icon
        6. Directing Users Back to the Play Store
        7. The Android Licensing Service
        8. Using ProGuard for Optimization, Fighting Piracy
        9. Preparing Your .apk File for Uploading
      3. Uploading Your Application
        1. Graphics
        2. Listing Details
        3. Publishing Options
        4. Contact Information
        5. Consent
      4. User Experience on Google Play Store
      5. Beyond Google Play Store
      6. References
      7. Summary
    42. Index