Asynchronous Android

Book description

As an Android developer you know you’re in a competitive marketplace. This book can give you the edge by guiding you through the concurrency constructs and proper use of AsyncTask to create smooth user interfaces.

  • Learn how to use Android's high-level concurrency constructs to keep your applications smooth and responsive
  • Leverage the full power of multi-core mobile CPUs to get more work done in less time
  • From quick calculations to scheduled downloads, each chapter explains the available mechanisms of asynchronous programming in detail

In Detail

With more than a million apps available from Google Play, it is more important than ever to build apps that stand out from the crowd. To be successful, apps must react quickly to user input, deliver results in a flash, and sync data in the background. Multithreading is the ability of an operating system to execute different parts of a program, called threads, simultaneously. It is one of the major techniques used for parallel computing and concurrency.

This book provides a step-by-step tutorial which will explain how Asynchronous Tasks and Handlers are used for performing multithreading with Android to improve the performance of Android applications. This book also guides you through the concurrency constructs provided by the Android platform, illustrating the applications, benefits, and pitfalls of each.

In this book, you'll learn how to use AsyncTask correctly to perform operations in the background, keeping user interfaces running smoothly while avoiding treacherous memory leaks. It will also help you discover Handler, HandlerThread, and Looper, the related and fundamental building blocks of asynchronous programming in Android.

This book will help you to build well-behaved applications with smooth and responsive user interfaces. By the end of this book, you will be able to delight your users with speedy results and data that is always fresh.

Table of contents

  1. Asynchronous Android
    1. Table of Contents
    2. Asynchronous Android
    3. Credits
    4. About the Author
    5. About the Reviewers
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Building Responsive Android Applications
      1. Introducing the Dalvik Virtual Machine
        1. Memory sharing and the Zygote
      2. Understanding the Android thread model
        1. The main thread
        2. Unresponsive apps and the ANR dialog
        3. Maintaining responsiveness
        4. Concurrency in Android
          1. Correctness issues in concurrent programs
          2. Liveness issues in concurrent programs
          3. Android-specific concurrency issues
            1. The Activity lifecycle
            2. Manipulating the user interface
        5. Android-specific concurrency constructs
      3. Summary
    9. 2. Staying Responsive with AsyncTask
      1. Introducing AsyncTask
      2. Declaring AsyncTask types
      3. Executing AsyncTasks
      4. Providing feedback to the user
        1. Providing progress updates
      5. Canceling AsyncTask
      6. Handling exceptions
      7. Controlling the level of concurrency
      8. Common AsyncTask issues
        1. Fragmentation issues
        2. Activity lifecycle issues
          1. Handling lifecycle issues with early cancellation
          2. Handling lifecycle issues with retained headless fragments
      9. Applications of AsyncTask
      10. Summary
    10. 3. Distributing Work with Handler and HandlerThread
      1. Understanding Looper
      2. Building responsive apps with Handler
        1. Scheduling work with post
          1. Canceling a pending Runnable
        2. Scheduling work with send
          1. Canceling pending Messages
          2. Composition versus Inheritance
          3. Multithreaded example
        3. Sending Messages versus posting Runnables
        4. Building responsive apps with HandlerThread
      3. Handler programming issues
        1. Leaking implicit references
        2. Leaking explicit references
      4. Applications of Handler and HandlerThread
      5. Summary
    11. 4. Asynchronous I/O with Loader
      1. Introducing Loaders
      2. Building responsive apps with AsyncTaskLoader
      3. Building responsive apps with CursorLoader
      4. Combining Loaders
      5. Applications of Loaders
      6. Summary
    12. 5. Queuing Work with IntentService
      1. Introducing Service and IntentService
      2. Building responsive apps with IntentService
        1. Handling results
          1. Returning results with PendingIntent
          2. Posting results as system notifications
      3. Applications of IntentService
        1. HTTP uploads with IntentService
        2. Reporting progress
      4. Summary
    13. 6. Long-running Tasks with Service
      1. Building responsive apps with Service
        1. Controlling concurrency with Executors
        2. Returning results with Messenger
      2. Communicating with Services
        1. Direct communication with local Services
        2. Broadcasting results with Intents
        3. Detecting unhandled broadcasts
      3. Applications of Services
      4. Summary
    14. 7. Scheduling Work with AlarmManager
      1. Scheduling alarms with AlarmManager
        1. Canceling alarms
        2. Scheduling repeating alarms
      2. Handling alarms
        1. Handling alarms with Activities
        2. Handling alarms with BroadcastReceiver
          1. Doing work with BroadcastReceiver
          2. Doing background work with goAsync
        3. Handling alarms with Services
      3. Staying awake with WakeLocks
      4. Applications of AlarmManager
      5. Summary
    15. Index

Product information

  • Title: Asynchronous Android
  • Author(s): Steve Liles
  • Release date: December 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781783286874