Make Your App Work with Android Wear in 4 Easy Steps

make-your-app-work-with-android-wear-in-4-easy-steps-0

This July, at its annual developer conference, Google introduced the full Android Wear SDK and now developers can finally start building awesome smartwatch applications. Want to learn how to make your existing apps work with Android Wear? Then read on!

Unless you’ve spent the last year on a deserted island, you’ve probably heard about Android Wear. It’s an extension of the Android operating system for wearable devices. Although there should be many different wearable devices in the future, the industry is focused on smartwatches at the moment.

Moto 360

Several big players have already launched their devices that support the Wear SDK – for example LG with its G watch or Samsung with Gear 2. Probably the most anticipated smartwatch, the one we use in visual illustrations for this article, is Moto 360 by Motorola Mobility. Luckily for all of us geeks out there, it should be launched in a few weeks.

If you are a developer, Android Wear gives you a chance to improve mobile experience and make your applications more accessible for users. If you want, you can develop Android Wear applications in the same way you develop smartphone applications, but SDK also allows you to extend your current applications. In this blog post, I will show you how to extend your application notifications to Android Wear and add some cool wear actions.

What are we modifying?

I’ll be modifying one of our existing apps called the Queueing app.

This app was developed for Erste & Steiermärkische Bank and it allows anyone with a smartphone to find the nearest branch office and remotely reserve a spot in the queue. The Queueing app can send a notification when users are next in line so they can head to the bank and grab their spot. If users change their mind, they have an option to delete their ticket and leave the queue. In order to use these features, they need to take out their mobile phones and open the app.

Queueing app on mobile phone and Android wear

Android wear makes things a lot easier

With Android wear, we can give our users the ability to use these features without reaching for their phones. We are going to modify the existing application and users with a smartwatch will get a notification on their wrists with information on how many people are in front of them.

Notifications are a cool addition, but we can do better. We will also add two more custom wear actions:

  • ”Snooze” notification – If users swipe the notification to the right, they will be offered the possibility to “snooze” it. For example, if there are 9 people in front of you, you can choose to be reminded when you’re 6th in line.
  • Cancel ticket – By swiping to the right once more, users will be able to cancel their current ticket and remove themselves from the queue.

The main idea behind these actions is to give our users the ability to interact with our application without the need to pull their smartphones out of their pockets. Just bear in mind that users will be able to use these actions only if they are not next in line. If they are, they will only get a notification indicating that there are no more people in front of them.

Modifying the existing app

Here is what you need to do in order to get the most out of Android Wear notifications:

1. Add the latest support library to your application’s build.gradle file

I hope you use Android Studio because the Eclipse is dead.

	
	dependencies {
    compile ’com.android.support:support-v4:20.0+’
}

2. Add Android wear specific actions to existing notification

We are using NotificationCompat.Builder class because, when building with this class, the system takes care of displaying notifications properly, whether they appear on a phone or smartwatch.

	
	//standard notification code
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.icon_notification_queueing_seethrough)
.setContentTitle(mContext.getString(R.string.redomat))
.setContentText(message);

//Actions "Snooze" notification and delete ticket (remove from queue) are only available if user is not next in line
if (!getIsUserNextInLine()) {

    //User click on action triggers broadcast which is received by WearActionReceiver.class
    //Put notification id and flag WEAR_ACTION. 
    //Using these parameters WearActionReceiver.class will know which action was clicked
    Intent notifyNextTimeIntent = new Intent(mContext, WearActionReceiver.class);
    notifyNextTimeIntent.putExtra(WearActionReceiver.NOTIFICATION_ID_STRING, NOTIFICATION_ID);
    notifyNextTimeIntent.putExtra(WearActionReceiver.WEAR_ACTION, WearActionReceiver.SNOOZE_NOTIFICATION);

    PendingIntent pendingIntentNotify = PendingIntent.getBroadcast(mContext, WEAR_REQUEST_CODE, notifyNextTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //text shown in notification
    String notifyAgainText = String.format(mContext.getString(R.string.notification_next_in_line), getNumberInLine());

    //Wear action - this action will be shown only on Android Wear devices
    //Set action icon, text and pending intent which will be executed on click
    //When user clicks on this icon he will "snooze" notification
    NotificationCompat.Action actionNotifyNextTime = new NotificationCompat.Action.Builder(R.drawable.ic_launcher, notifyAgainText, pendingIntentNotify).build();

    //The same as Intent for "snooze" but this time set another flag
    Intent cancelTicketIntent = new Intent(mContext, WearActionReceiver.class);
    cancelTicketIntent.putExtra(WearActionReceiver.NOTIFICATION_ID_STRING, NOTIFICATION_ID);
    cancelTicketIntent.putExtra(WearActionReceiver.WEAR_ACTION, WearActionReceiver.CANCEL_TICKET);

    PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(mContext, WEAR_REQUEST_CODE_2, cancelTicketIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     //When user clicks on this icon he will cancel his ticket and remove himself from the queue
     NotificationCompat.Action actionCancel =
            new NotificationCompat.Action.Builder(R.drawable.actionbar_icon_delete,
                    mContext.getString(R.string.cancel_ticket), pendingIntentCancel)
                    .build();

    //Create new WearableExtender object and add actions
    NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
    extender.addAction(actionNotifyNextTime);
    extender.addAction(actionCancel);

    //Extend Notification builder
    mBuilder.extend(extender);

}

//Get notification manager
NotificationManager mNotificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

//show notification
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

3. Add Broadcast Receiver

This class will receive the clicked wear action and either “snooze” the notification or remove the user from the queue. It will also dismiss notifications on Android Wear.

	
	public class WearActionReceiver extends BroadcastReceiver {

    public static final String NOTIFICATION_ID_STRING = "NotificationId";
    public static final String WEAR_ACTION = "WearAction";
    public static final int SNOOZE_NOTIFICATION = 1;
    public static final int CANCEL_TICKET = 2;

    @Override
    public void onReceive (Context context, Intent intent) {

        if (intent != null) {

            int notificationId = intent.getIntExtra(NOTIFICATION_ID_STRING, 0);
            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(notificationId);

            int action = intent.getIntExtra(WEAR_ACTION, 0);

            switch (action) {
                case SNOOZE_NOTIFICATION:
                    //Code for notification snooze
                    break;
                case CANCEL_TICKET:
                    //code for removing the user from the queue
                    break;
                default:
                    break;
            }
       }
    }
}

4. Declare WearActionReceiver in the manifest file

	
	 <receiver android:name="co.infinum.WearActionReceiver"/>
Queueing app on Android wear

Updates – Make them happen

As you can see, with just a few extra lines of code, we gave our users an option to interact with our application without reaching for their phones. Considering the fact that implementing support for Android Wear is really easy, I believe that every Android dev should be aiming to update their apps as soon as possible.

Maybe now you are thinking; “Heck, why would I do that when the majority of users won’t buy a fancy smartwatch?” That’s a valid concern, especially when we know that the current generation of smartwatches costs between $200 and $300.

But like with any other technology, the price of Android Wear devices will go down and they will become more widespread. Making sure your app supports this innovative technology is a smart investment for the future.