Presentation/demo held at Google Developer Group Oslo DevFest 2014
Abstract:
This talk will show you how to build an Android Wear app. Thomas will start out with a plain notification and show you how to add features until you have a full-fledged card UI. We will discuss the role of the phone and the watch and how to communicate between them. As a case study he will use a prototype for a news alert app he's working on - there will be plenty of code examples and demos.
Thomas Oldervoll has worked 5 years for Google in Mountain View and 9 years for Zenior in Oslo. He likes building things, in particular machine learning, scalable backend systems and Android apps.
7. Plain old notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.sunrise32)
.setContentTitle(story.title)
.setContentText(story.contents)
.setContentIntent(createReadMoreIntent(story))
.setAutoCancel(true);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(0, notificationBuilder.build());
8. Add an image and it looks like a card
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.sunrise32)
.setContentTitle(story.title)
.setContentText(story.contents)
.setContentIntent(createReadMoreIntent(story))
.setAutoCancel(true);
Bitmap image = loadImage(story.image);
if (image != null) {
notificationBuilder.setLargeIcon(image);
}
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(0, notificationBuilder.build());
9. Add pages
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender();
for (Story subStory : story.subStories) {
Notification storyPageNotification =
new NotificationCompat.Builder(context)
.setContentTitle(bold(subStory.title))
.setContentText(subStory.contents)
.build();
wearableExtender.addPage(storyPageNotification);
}
notificationBuilder.extend(wearableExtender);
10. Add a separate page for just the image
if (image != null) {
Notification imageOnlyNotification =
new NotificationCompat.Builder(context)
.extend(new NotificationCompat.WearableExtender()
.setBackground(image)
.setHintShowBackgroundOnly(true))
.build();
wearableExtender.addPage(imageOnlyNotification);
}
11. Show more content in the phone
notification
NotificationCompat.BigTextStyle style =
new NotificationCompat.BigTextStyle();
style.bigText(story.contents);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.sunrise32)
.setContentTitle(story.title)
.setContentText(story.contents)
.setContentIntent(createReadMoreIntent(story))
.setAutoCancel(true)
.setStyle(style);
12. News alert app
• Push news alerts as they happen
• By default push most important stories
• Allow user to subscribe to stories of interest
13. Who detects when
something happened?
• The watch? No, doesn’t have Internet
• The phone? No, would kill battery
• App Engine? No, can’t use Web Sockets
• Google Cloud Mananged VMs? Yes.
15. Adding actions
notificationBuilder.addAction(
createFollowAction(story.channel, notificationId));
pr private NotificationCompat.Action createFollowAction(
String channel, int notifcationId) {
int icon = R.drawable.ic_action_important;
String title = context.getString(R.string.unfollow);
String action = FetcherService.ACTION_UNFOLLOW;
Intent intent = new Intent(context, FetcherService.class);
intent.setAction(action + "/" + channel + "/" + notifcationId);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
return new NotificationCompat.Action(icon, title, pi);
}
16. When do you need more
than notifications?
• More advanced UIs? Can send custom activities in
notifications
• Launch from Wear
• Use Wear sensor
• Run with no phone connection
17. What can Wear apps do?
• Pretty much anything an Android app can do - but
keep the form factor in mind
• No Internet, no webkit
• APIs for sending and receiving messages and data
to/from phone
22. Wear-enabling your app
• Don’t squeeze the phone UI onto the wrist!
• Instead, enrich your notifications. This will make
your app better on the phone as well.
• Use your app on a watch every day