O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending Notifications

Here’s an opportunity to introduce yet another system service—this time the Notification service. We worked hard to have our Updater service run in the background and get the latest status updates, but what’s the point of all this work if the user is not made aware that there’s something new to look at? A standard Android UI approach to this would be to post a notification to the notification bar up at the top of the screen. To do that, we use the Notification system service.

We’re going to make the Updater service responsible for posting the notifications, since it is the part of the app that knows of new statuses in the first place. To do that, we’ll get the reference to the system Notification service, create a new Notification object, and update it with the latest information. The notification itself will contain a pending intent so that when the user clicks on it, it takes the user to Timeline activity to view the latest status updates. Example 13-15 shows the new code.

Example 13-15. UpdaterService.java with Notifications

package com.marakana.yamba8; import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.util.Log; public class UpdaterService extends IntentService { private static final String TAG = "UpdaterService"; public static final String NEW_STATUS_INTENT = "com.marakana.yamba.NEW_STATUS"; public static final String NEW_STATUS_EXTRA_COUNT ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required