The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #16 - Updating the Book

The app is designed to ship a copy of the book’s chapters as assets, so a user can just download one thing and get everything they need: book and reader.

However, sometimes books get updated. This is a bit less likely with the material being used in this tutorial, as it is rather unlikely that H. G. Wells will rise from the grave to amend The War of the Worlds. However, other books, such as Android developer guides written by balding guys, might be updated more frequently.

Most likely, the way you would get those updates is by updating the entire app, so you get improvements to the reader as well. However, another approach would be to be able to download an update to the book as a separate ZIP file. The reader would use the contents of that ZIP file if one has been downloaded, otherwise it will “fall back” to the copy in assets. That is the approach that we will take in this tutorial, to experiment a bit with Internet access and services. Along the way, we will use Retrofit to call a Web service (of sorts) to find out if an update is available.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Step #1: Adding a Stub DownloadCheckService

There are a few pieces to our download-the-book-update puzzle:

To address the first puzzle piece — determining if there is an update available — we can use an IntentService. That makes it easy for us to do the work not only in the background from a threading standpoint, but also be able to use it either from the UI or from some sort of background-work scheduler. So, let’s add a DownloadCheckService to our project.

Right-click over the com.commonsware.empublite package in your java/ directory and choose New > Service > “Service (IntentService)” from the context menu. Fill in DownloadCheckService as the class name and uncheck the “helper methods” checkbox. Click Finish to generate the DownloadCheckService class and add an entry for you to the manifest.

Then, replace the generated implementation of DownloadCheckService with:

package com.commonsware.empublite;

import android.app.IntentService;
import android.content.Intent;

public class DownloadCheckService extends IntentService {
  public DownloadCheckService() {
    super("DownloadCheckService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
  }
}

Step #2: Tying the Service Into the Action Bar

The preview of this section is in the process of being translated from its native Klingon.

Step #3: Defining Our Event

The preview of this section is [REDACTED].

Step #4: Defining Our JSON

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Step #5: Defining Our Retrofit Interface

The preview of this section is sleeping in.

Step #6: Retrieving Our JSON Via Retrofit

The preview of this section was traded for a bag of magic beans.

Step #7: Downloading the Update

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Step #8: Unpacking the Update

The preview of this section was lost due to a rupture in the space-time continuum.

Step #9: Using the Update

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

In Our Next Episode…

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.