Often times, applications will perform some background operation—maybe while the app is running or maybe triggered on a scheduled alarm. The user won’t know what’s going on, though, unless they are informed of some event. Luckily, Android has an easy notification system that allows applications to display a message in the status bar and provide informative details to the user when something important occurs.
In this tutorial, we’ll extend the existing unnamed “TutList” application to notify the user when the scheduled download of tutorials has taken place. When the user taps on the notification in the status bar, the application will be launched. This way, we use notifications to provide feedback to the user that something has happened, while also reminding he user of the existence of the application and that fresh content is available.
Step 0: Getting Started
This tutorial assumes you start where our last “TutList” tutorial left off. You can download that code and work from it, or you can simply download or view the code provided with this tutorial and follow along. The choice is yours. We’re on Version Code 9. The “tags” view of the source viewer has each version, for convenience.
Step 1: Android Notifications
The Android notification system is highly praised by users and developers. It’s easy to use, flexible, and convenient. This tutorial will not cover every detail of the notification system. Instead, we’ll focus on getting specific notifications working for the TutList application.
Generally speaking, notifications in the status bar at the top of the users screen (or the bottom on tablets running Android 3.0) appear as an icon and some short text. When the status bar is “pulled down”, the user can see more notification details. These details can be drawn in a default way or they can be drawn using a custom RemoveViews object if you want to get fancy. Additionally, notifications can play sounds or flash colorful lights. Notifications can also display once, display persistently (for instance, to update the progress of an ongoing task), or display a count of items (for instance, to display the number of new emails).
We will create a simple notification that informs the user when the list of tutorials, in XML format, has been downloaded and parsed.
Let’s start by getting an instance of the NotificationManager object, the base object with which notifications are handled. We’ll start by adding code to the onPostExecute() method of the DownloaderTask inner class found in the TustListDownloaderService.
Read more: MobileTuts