Asynchronous Android Programming - Second Edition

Book description

Unlock the power of multi-core mobile devices to build responsive and reactive Android applications

About This Book

  • Construct scalable and performant applications to take advantage of multi-thread asynchronous techniques
  • Explore the high-level Android asynchronous constructs available on the Android SDK
  • Choose the most appropriate asynchronous technique to implement your next outstanding feature

Who This Book Is For

This book is for Android developers who want to learn how to build multithreaded and reliable Android applications using high-level and advanced asynchronous techniques and concepts. No prior knowledge of concurrent and asynchronous programming is required. This book will also be great for Java experts who are new to Android.

Whether you are a beginner at Android development or a seasoned Android programmer, this book will guide you through the most basic and advanced asynchronous constructs used in Android programming.

What You Will Learn

  • Get familiar with the android process model and low-level concurrent constructs delivered by the Android SDK
  • Use AsyncTask and loader framework to load data in the background, delivering progress results in the meantime
  • Create services that interact with your activity without compromising the UI rendering
  • Learn the working of Android concurrency on the Native Layer
  • Interact with nearby devices over Bluetooth and WiFi communications channels
  • Create and compose tasks with RxJava to execute complex asynchronous work in a predictable way
  • Get accustomed to the use of the Android Loader construct to deliver up-to-date results

In Detail

Asynchronous programming has acquired immense importance in Android programming, especially when we want to make use of the number of independent processing units (cores) available on the most recent Android devices. With this guide in your hands you'll be able to bring the power of Asynchronous programming to your own projects, and make your Android apps more powerful than ever before!

To start with, we will discuss the details of the Android Process model and the Java Low Level Concurrent Framework, delivered by Android SDK. We will also guide you through the high-level Android-specific constructs available on the SDK: Handler, AsyncTask, and Loader. Next, we will discuss the creation of IntentServices, Bound Services and External Services, which can run in the background even when the user is not interacting with it. You will also discover AlarmManager and JobScheduler APIs, which are used to schedule and defer work without sacrificing the battery life. In a more advanced phase, you will create background tasks that are able to execute CPU-intensive tasks in a native code-making use of the Android NDK. You will be then guided through the process of interacting with remote services asynchronously using the HTTP protocol or Google GCM Platform. Using the EventBus library, we will also show how to use the Publish-Subscribe software pattern to simplify communication between the different Android application components by decoupling the event producer from event consumer.

Finally, we will introduce RxJava, a popular asynchronous Java framework used to compose work in a concise and reactive way. Asynchronous Android will help you to build well-behaved applications with smooth responsive user interfaces that delight the users with speedy results and data that's always fresh.

Style and approach

This easy-to-follow guide is full of code examples of real-world use cases. Each asynchronous topic is explained sequentially, from the most basic and low-level to the more advanced, using concise and effective language. Some lifecycle flows and concepts feature illustrations to help you understand the complex interactions between Android entities.

Table of contents

  1. Asynchronous Android Programming Second Edition
    1. Table of Contents
    2. Asynchronous Android Programming Second Edition
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    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. Asynchronous Programming in Android
      1. Android software stack
        1. Dalvik runtime
        2. ART runtime
        3. Memory sharing and Zygote
      2. Android process model
        1. Process ranks
        2. Process sandboxing
      3. Android thread model
        1. The main thread
        2. The Application Not Responding (ANR) dialog
        3. Maintaining responsiveness
      4. Concurrency in Android
        1. Correctness issues in concurrent programs
        2. Liveness issues in concurrent programs
        3. Thread coordination
        4. Concurrent package constructs
          1. Executor framework
      5. Android primary building blocks
        1. Activity concurrent issues
        2. Manipulating the user interface
        3. Service concurrent issues
        4. Started services issues
        5. Bound services issues
      6. Service in a separate process
        1. Broadcast receiver concurrent issues
        2. Android concurrency constructs
      7. Summary
    9. 2. Performing Work with Looper, Handler, and HandlerThread
      1. Understanding Looper
        1. Understanding Handler
        2. Sending work to a Looper
        3. Scheduling work with post
        4. Using Handler to defer work
        5. Leaking implicit references
        6. Leaking explicit references
        7. Updating the UI with Handler
        8. Canceling a pending Runnable
        9. Scheduling work with send
        10. Cancelling pending messages
        11. Composition versus inheritance
        12. Multithreading with Handler and ThreadHandler
          1. Looper message dispatching debugging
        13. Sending messages versus posting runnables
        14. Applications of Handler and HandlerThread
      2. Summary
    10. 3. Exploring the AsyncTask
      1. Introducing AsyncTask
      2. Declaring AsyncTask types
      3. Executing AsyncTasks
      4. Providing indeterministic progress feedback
      5. Providing deterministic progress feedback
      6. Canceling an AsyncTask
        1. AsyncTask Execution State
      7. Handling exceptions
      8. Controlling the level of concurrency
      9. Common AsyncTask issues
        1. Fragmentation issues
        2. Memory leaks
        3. Activity lifecycle issues
          1. Handling lifecycle issues with early cancellation
          2. Handling lifecycle issues with retained headless fragments
      10. Applications of AsyncTask
      11. Summary
    11. 4. Exploring the Loader
      1. Introducing Loaders
        1. Loader API
          1. Loader
          2. Loader Manager
          3. LoaderManager.LoaderCallbacks
        2. Loader lifecycle
      2. Loading data with Loader
      3. Building responsive apps with AsyncTaskLoader
      4. Building responsive apps with CursorLoader
      5. Combining Loaders
      6. Applications of Loader
      7. Summary
    12. 5. Interacting with Services
      1. Introducing Service
      2. Started service
        1. Building responsive apps with IntentService
        2. Handling results
        3. Posting results with PendingIntent
        4. Posting results as system notifications
        5. Applications of IntentService
          1. HTTP uploads with IntentService
          2. Reporting progress
      3. Bound Service
        1. Communicating with a Local Service
        2. Broadcasting results with intents
        3. Detecting unhandled broadcasts
      4. Applications of Services
      5. Summary
    13. 6. Scheduling Work with AlarmManager
      1. Introducing AlarmManager
      2. Scheduling alarms with AlarmManager
        1. Setting alarms in recent Android versions
        2. Testing your alarms in Doze Mode
        3. Setting a Window alarm
        4. Debugging AlarmManager alarms
      3. Canceling alarms
      4. Scheduling repeating alarms
      5. Scheduling an alarm clock
      6. Handling alarms
      7. Handling alarms with Activities
      8. Handling alarms with BroadcastReceiver
        1. Working with BroadcastReceiver
        2. Asynchronous work with goAsync
      9. Handling alarms with Services
        1. Staying awake with WakeLocks
      10. Resetting alarms after a system reboot
      11. Applications of AlarmManager
      12. Summary
    14. 7. Exploring the JobScheduler API
      1. Introduction to JobScheduler
      2. Setting running criteria
      3. Scheduling a job
      4. Implementing the JobService
      5. Listing pending jobs
      6. Canceling a job
      7. Scheduling a periodic job
      8. Applications of the JobScheduler
      9. Summary
    15. 8. Interacting with the Network
      1. Introducing Android HTTP clients
        1. AndroidHttpClient
        2. HttpURLConnection
      2. Performing HTTP requests asynchronously
        1. Retrieving a text response
        2. Interacting with JSON web APIs
          1. Converting Java objects to JSON
        3. Interacting with XML web APIs
          1. Converting Java objects to XML
          2. Converting XML to Java objects
      3. Customizing HTTP timeouts
      4. Communicating securely over SSL sessions
      5. Summary
    16. 9. Asynchronous Work on the Native Layer
      1. Introduction to JNI
        1. Android NDK (Native Development Kit)
      2. Calling C functions from Java code
      3. Calling C++ functions from native code
      4. Accessing Java objects from native code
      5. Executing native background work on Java threads
      6. Executing asynchronous work on a native thread
        1. Attaching and detaching native threads from JVM
        2. JNI references explained
        3. Interacting with UI from native threads
        4. Starting the native threads
        5. Stopping the native threads
      7. Handling Java exceptions in the native layer
      8. Interacting with a Java monitor from native code
        1. Wrapping native data objects
      9. Summary
    17. 10. Network Interactions with GCM
      1. Introduction to GCM
      2. Setting up and configuring GCM for your application
        1. Registering the GCM Receiver
        2. Setting up a registration service
        3. InstanceID listener
      3. Receiving downstream messages
      4. Receiving messages from topic
      5. Sending upstream messages
      6. GcmListenerService delivery callbacks
      7. Executing tasks with GCM Network Manager
        1. Building a one shot task
      8. Summary
    18. 11. Exploring Bus-based Communications
      1. Introduction to bus-based communication
      2. EventBus library
      3. Defining events
      4. Submitting events
      5. Registering sbscribers
      6. Thread mode
      7. Posting sticking events
      8. Removing sticky events
      9. Summary
    19. 12. Asynchronous Programing with RxJava
      1. Introduction to RxJava
        1. Cold versus Hot Observable
      2. RxJava setup
      3. Creating Observables
      4. Transforming Observables
      5. Understanding Schedulers
      6. Performing IO operations with Schedulers
      7. Canceling subscriptions
      8. Composing Observables
      9. Monitoring the event stream
      10. Combining Observables
      11. Observing UI Events with RxJava
      12. Working with Subjects
      13. Summary
    20. Index

Product information

  • Title: Asynchronous Android Programming - Second Edition
  • Author(s): Helder Vasconcelos
  • Release date: July 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781785883248