AlarmManager / Android Wear / Notification / Notification.Builder / Notification.WearableExtender

Match Timer – Part 4

Previously in this series on developing Match Timer (for timing football matches on Android Wear) we’ve looked at the timing engine and the BroadcastReceiver which is used to register alarms and handle events both from those alarms and from user actions. In this article we’ll turn our attention to notifications which are the primary interface from the handlers in the BroadcastReceiver to the user.

matchtimerOn Wear Notifications are the primary mechanism for updating the user and providing easy access to that information, and that is very true of how Match Timer is implemented. During normal operation we will display a Notification which will show the number of minutes elapsed, but when the user swipes in to the notification, we want to display detailed time information with per-second accuracy, and all of this can be achieved via the Notification framework.

Notifications on Wear are based on standard Android Notifications, but there are some extensions for Wear which we can make really good use of. Forst let’s have a look at the NotificationBuilder code:

The first thing that we do is create a PendingIntent for our MatchTimerNotificationActivity which will contain the detailed, per-second accuracy Views. We then create a standard Notification.Builder and a Notification.WearableExtender (which is provided by the Wearable support library).

One thing that WearableExtender permits us to do is provide a custom background, so we add a graphic of a football field. We also set a Display Intent which is a really powerful feature of WearableExtender which allows us to display an Activity as part of our notification. When the device is asleep, of the watch-face Activity is active, then just a simple Notification title will be shown, but when the user swipes in to the notification, they will be shown our custom Activity sitting on top of the background graphic, with the Notification Icon showing:

matchtimer

Next we add some custom actions depending on the timer state. If it is paused we want to include Resume & Stop actions; if it is running we want to include Pause & Stop actions; and if it is stopped we want to include Start and Reset actions. These actions are added to WearableExtender and will each fire a broadcast which will be handled by MatchTimerReceiver that we looked at in the previous article in this series.

Next we set some standard Notification options for the Notification title (which will be our minute-accurate information), the icon, style, and whether the Notification is ongoing. I have chosen to make it an ongoing notification when the timer is running or paused to reduce the chance of the user accidentally dismissing it – it requires the timer to be stopped before the notification can be dismissed. That said, the timer is pretty robust because of its state being stored to SharedPreferences, and it even survives the device being restarted.

I have also choses to give the Notification MAX priority to try and ensure that it stays near the top of the Notification list to ensure that the user can get to it quickly while the timer is active.

In the next article we’ll take a look at MatchTimerNotificationActivity which displays our details timing information.

Match Timer is available on Google Play.


Get it on Google Play

© 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Match Timer – Part 4 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *