651.288.7000 info@intertech.com

Getting your Android app on wearable devices is easy.  If your app creates notifications, it already is on wearables due to Bridged Notifications.  This is a notification that is created on the handheld, and Android automatically displays it on paired wearable devices.  Android provides ways to extend bridged notifications with wearable specific features such as additional pages and custom actions that can collect voice input and provide that input to an Intent.

This will be demonstrated by extending the Intertech Time Entry application.  If a user has not entered enough time by the end of the week, a reminder message is sent from the application server to the user’s devices.  For Android devices, this is done using Google Cloud Messaging (see our series on Push Notifications).  This message simply contains text, which is displayed as an Android notification.

In this second part of our Android Wear developer tutorial, I am going to extend this notification with enhancements for wearable devices.  When the message is received, we will check and see how much time has been entered for the week.  If time entered is greater than zero, we will add a second page to the wearable notification to confirm the amount of time entered.  If no time has been entered, we will extend the notification to present quick actions on the wearable (for example, copy time from last week).

Setup

To ensure that wearable features of bridged notifications work properly, use these compatibility classes from the support library:

  • NotificationCompat
  • NotificationManagerCompat
  • NotificationCompat.WearableExtender

To use these classes, you need to include the support library in your module.  Add this to your build.gradle file:

With the compatibility classes imported, we can use them to build a notification:

In this example, we’re not doing anything specific for wearable devices.  Using the compatibility classes ensures that the notification will work on wearables.  Note that we are setting a pending intent by calling onContentIntent.  On the handheld, clicking the notification will start this Intent.  On the wearable, the user can swipe left to see the “Open” action, which will also start the intent on the handheld.

Extending for Wear

Bridged Notifications can be extended with features specific to wearables.  This is done using the NoticationCompat.WearableExtender class.  We are going to use this class to extend our notification.  As mentioned above, we are going to extend our notification in two ways:

  1. If time has been entered for the week, add a second page that confirms for the user how much time has been entered.
  2. If no time has been entered for the week, add an action to collect input.  The input options will correspond to functionality that already exists within the Time Entry mobile app:
    1. Copy Last Week
    2. Sick This Week
    3. Vacation This Week

When one of these actions is selected, we can invoke the existing logic to perform these functions.

Adding a page to a Notification

To add a page to a notification, you create a new notification for the additional page, and use WearableExtender to add it to the original notification:

When the notification is issued, it will have a second page on wearable devices:

First Page

First Page

Second Page

Second Page

Adding Actions

As mentioned earlier, if our notification has a PendingIntent set using setContentIntent, on the wearable the notification can be swiped left to access the “Open on phone” button.  Additional actions can be added by using the addAction method.  On a handheld device, these actions appears as buttons along with the notification.  On a wearable, the actions are available in the usual way: by swiping left.  In this case, I want to add actions that are only available on wearable devices.  The NotificationCompat.WearableExtender class also has an addAction method which does exactly that.

When you add an Action to a notification, you have the option to add a RemoteInput to collect input from the user.  This input will be available to the target component via an Intent object.  For our current case, we will invoke an IntentService that will read the provided input and perform the selected function (copy last week, sick, or vacation).  An IntentService is perfect for our case because the onHandleIntent method is executed in a background thread, so we are free to make synchronous calls to our REST service to perform the specified action.

RemoteInput by default will capture free from voice input, so how can we be sure that the user selects one of the three specified options?  Lucky for us, the RemoteInput builder let’s you set pre-defined choices using the setChoices method.

Here are the steps to create an action with remote input that will invoke our service:

  1. Create a RemoteInput with pre-defined options.
  2. Create a PendingIntent that invokes our IntentService.
  3. Create an Action for the PendingIntent, that uses the RemoteInput.
  4. Use WearableExtender to add the Action to the Notification.

Here’s the code that accomplishes this:

Note the following:

  • The reply options are loaded from a string array resource.  In the IntentService that receives the input, we will be checking to see which option was selected.  By defining the strings in a resource file, we can be sure that we are using the same values in both locations.
  • The RemoteInput.Builder constructor takes a String argument, which is the key used to get the result from the Intent in our service.  Once again, we are using a resource to ensure that the same value is used in both locations.
  • Calling setAllowFreeFormInput(false) disables free form input.  This means that only our provides responses can be selected.  It also effectively disables voice input, since the user must click on one of the options provided.

We have everything in place for allowing quick actions to be selected from our notification on wearable devices.  Here is what that looks like:

First Page

First Page

Action (swipe left)

Action (swipe left)

 

Clicking on the “Enter Time” action brings up the input:

Quick Actions

Quick Actions

When an option is selected, Android takes care of invoking the PendingIntent on the handheld app and confirming that the the intent was started successfully (with a gentle vibrate and confirmation).  Since our pending intent refers to an IntentService, the onHandleIntent method of the service is called.  Here is basics of what that method looks like:

We get the selected option from the Intent (using RemoteInput.getResultsFromIntent), and then perform the necessary action.  We also use NotificationManagerCompat to dismiss the notification.

Wrap Up

We have successfully extended our mobile app with features for Android Wear through part 2 of this Android Wear developer tutorial.  When the end of the week is near, I am reminded to enter my time for the week.  If I have not yet entered time, I am provided with some quick actions that can be performed from my Android smart watch.  No need to take my phone out of my pocket.

I think this a good example of how Android Wear can improve a mobile application.  It provides information that is timely and relevant, and certain actions can be performed with minimal user interaction.  What’s more, we accomplished all of this without having to write a separate application for wearable devices!

Indeed, you can accomplish a lot using bridged notifications and WearableExtender.  However, there may be times when this is not enough, and you want the power and flexibility of a standalone app that runs on wearable devices.  The next post in this Android Wear developer tutorial series covers how to write standalone wearable applications.

If you’re just finding this Android Wear Developer tutorial now, here are a couple links to the rest of the posts in the series:

Android Wear Developer Tutorial (Part 1) – Introduction and Overview

Android Wear Developer Tutorial (Part 3) – Creating a Standalone App

Android Wear Developer Tutorial (Part 4) – Wearable Only Notification With a Custom Layout

Android Wear Developer Tutorial (Part 5) – Tips and Tricks

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This