Updating the Android Status Bar

309

Chapter 14: Updating the Android Status Bar

➝ 12 This line sets the content of the expanded view with that standard Latest Event layout as provided by Android. For example, you could provide a custom XML layout to display. In this instance, I am not providing a custom layout; I’m simply providing the stock notification view. The setLatestEventInfo method accepts the following parameters: • context: this: The context to associate with the event info • contentTitle: getStringR.string.notifiy_new_task_ title: The title that goes into the expanded view • contextText: getStringR.string.notify_new_task_ message: The text that goes into the expanded view • contentIntent: pi: The intent to launch when the expanded view is selected ➝ 14 A bitwise-ored in setting the Notification object to include sound during the notification process. This forces the default notification sound to be played if the user has the notification volume on. ➝ 15 A bitwise-ored in setting the Notification object flag’s property that cancels the notification after it is selected by the user. ➝ 19 Casting the ID to an integer. The ID stored in the SQLite database is long; however, I am casting it to an integer. A loss of precision is happening. However, I highly doubt that this application would ever have more than 2,147,483,647 tasks set up which is the maximum number that an integer can store in Java. Therefore, this casting should be okay. The casting to an integer is necessary because the code on line 20 only accepts an integer as the ID for the notification. ➝ 20 Raises the notification to the status bar. The notify call accepts two parameters: • id: id: An ID that is unique within your application. • Notification: note: A Notification object that describes how to notify the user. Viewing the workflow The previous code allows the following workflow to occur: ✓ The user is active in another application, such as e-mail. ✓ A task is due and therefore the alarm fires. The notification is created in the status bar. 310 Part III: Creating a Feature-Rich Application ✓ The user can elect to slide down the status bar and select the notification or ignore it for now. If the user chooses to slide open the status bar and select an item, the pending intent within the notification will be activated. This in turn causes the ReminderEditActivity to open with the given row ID of the task. ✓ The notification is removed from the status bar. ✓ The task information is retrieved from the database and displayed on the form in the ReminderEditActivity. Adding string resources You may notice that you need to add the following two string resources: ✓ notify_new_task_message: I have set the value of this to “A task needs to be reviewed” This message is used as the message in the expanded view and is used as the ticker text when the notification first arrives. ✓ notify_new_task_title: I have set the value of this to “Task Reminder.” This message is used as the title for the expanded view. Updating a Notification At some time, you might need to update the view of your notification. Consider the following situation: You have some code that runs in the back- ground to see whether the tasks have been reviewed. This code checks to see whether any notifications are overdue. You decide that after the two-hour mark passes, you want to change the icon of the notification to a red-colored exclamation point and flash the LED quickly with a red color. Thankfully, updating the notification is a fairly simple process. If you call one of the notify methods with an ID that is currently active in the status bar, and with a new set of notification parameters, the notifica- tion is updated in the status bar. Therefore, you would simply create a new Notification object with the red icon as a parameter and call notify — which would update the notification. 311

Chapter 14: Updating the Android Status Bar