Using the Notification Manager
You use the Notification Manager to interact with the Android notification mechanism. Working with the Notification Manager is as simple as requesting it from the current context.
The following line of code obtains the NotificationManager
object from the getSystemService()
call of Context
:
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Creating a notification
The Task Reminder application needs a way to notify the user that a task needs attention, such as an alarm signifying that task. To set this notification on the status bar, you use the NotificationManager
.
In the doReminderWork()
method of the ReminderService
class, type the code shown in Listing 14-1.
Listing 14-1: Implementation of doReminderWork()
Long rowId = intent.getExtras().getLong(ReminderProvider.COLUMN_ROWID); →1
NotificationManager mgr =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE); →4
Intent notificationIntent = new Intent(this, ReminderEditActivity.class); →6
notificationIntent.putExtra(ReminderProvider.COLUMN_ROWID, rowId); →7
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); →9
Notification note=new Notification(android.R.drawable.stat_sys_warning,
getString(R.string.notify_new_task_message),
System.currentTimeMillis()); →14
note.setLatestEventInfo(this, getString(R.string.notify_new_task_title),
getString(R.string.notify_new_task_message), pi); →17
note.defaults ...
Get Android Application Development For Dummies, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.