Scheduling Work

Sometimes, we need to do work periodically, even at times when the user is not using the app. Android has many ways to try to accomplish this, and most have problems. That is because developers have a tendency to abuse this sort of capability (e.g., trying to do something every 5 seconds) in ways that drain the battery. Users get very irritated when their battery level keeps plummeting when they are not using their device. So, Google has made it increasingly more difficult to do this sort of background work.

If your needs are fairly casual — do a small bit of work every so often, with no particular concerns over the precise timing — WorkManager can handle the job. With WorkManager, we describe the work that we want to do and under what conditions that it should be done, including timing (e.g., every 15 minutes). WorkManager takes care of the rest. WorkManager is not guaranteeing anything about the timing, though, as Android tends to block background work after a while of the device being idle, to conserve battery. But, for periodic work, WorkManager is about as good as we can get nowadays, particularly for a purely device-side solution.

In this tutorial, we will integrate WorkManager, to periodically import the to-do items from our “Web service”.

This is a continuation of the work we did in the previous tutorial. The book’s GitLab repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Step #1: Defining a SwitchPreference

We need some way for the user to control whether this sort of periodic import should be happening. Some users might like it, and some users might not.

(for this sample app, no users should like it, since it will keep importing the same items over and over… but this is just a book sample)

One way to do that is to have another preference in our settings screen. In particular, Android offers CheckBoxPreference and SwitchPreference for this sort of on/off toggle.

We are going to need some string resources, as we did with the EditTextPreference. So, switch over to res/values/strings.xml and add these two strings:

  <string name="pref_import_title">Import periodically</string>
  <string name="import_key">doPeriodicImport</string>

Then, open res/xml/prefs.xml in Android Studio, switch to the “Code” view, and replace the XML with:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

  <EditTextPreference
    android:key="@string/web_service_url_key"
    android:selectAllOnFocus="true"
    android:title="@string/pref_url_title"
    app:defaultValue="@string/web_service_url_default" />
  <SwitchPreference
    android:defaultValue="false"
    android:key="@string/import_key"
    android:title="@string/pref_import_title" />
</PreferenceScreen>

This adds a SwitchPreference after our existing EditTextPreference.

As with the EditTextPreference, we have android:key, android:title, and android:defaultValue attributes. The first two point to our new string resources. And, we set the default value to false, so the user will need to opt into having this periodic work occur.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.