codelab - android auto notifications

18
Android Auto Code Lab Messaging API’s on Android Auto

Upload: paul-blundell

Post on 17-Aug-2015

292 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Codelab - Android Auto notifications

Android Auto Code Lab

Messaging API’s on Android Auto

Page 2: Codelab - Android Auto notifications

What you’ll learn

Android programming

Extend a notification so that it also shows on the car

Handle the user replying to your notification message

Page 3: Codelab - Android Auto notifications

What you’ll need

Android Studio version 1.0+

The sample code

A test device with Android Lollipop (5.0+)

An Android Auto compatible head unit

or the Android Auto Messaging Simulator

Page 4: Codelab - Android Auto notifications

Get the sample code

git clone https://github.com/novoda/dojos.git

or download source

http://surl.novoda.com/auto-code

The folder is: The

module is:

/androidauto-messaging /base

Page 5: Codelab - Android Auto notifications

Import the sample app

First, we will bootstrap with a very basic messaging app that simply

creates a notification.

1. Open Android Studio

2. Select the androidauto-messaging/base directory from the code

folder

(File > Import Project… > androidauto-messaging/base).

3. Click the Android Studio Run button.

Page 6: Codelab - Android Auto notifications

What does this app do?

● Clicking button sends a notification after 15 seconds

● IntentService - this is used to do work in the background

● Create a Notification using NotificationCompat.Builder

● Add an on-click action to the notification

Page 7: Codelab - Android Auto notifications

Extending a Notification for Android AutoCreate a new file "res/xml/automotive_app_desc.xml":

Right click in the "res" folder and select File -> New -> Android

Resource File

Page 8: Codelab - Android Auto notifications

Extending a Notification for Android AutoThen fill the dialog with the following info:

File name: automotive_app_desc

Resource type: XML

Root element: automotiveApp

Page 9: Codelab - Android Auto notifications

Extending a Notification for Android AutoReplace the content of the file you just created with the following

content. This declares that the application is using the Android Auto

Messaging API.

<?xml version="1.0" encoding="utf-8"?><automotiveApp> <uses name="notification"/></automotiveApp>

code-help-1.txt

Page 10: Codelab - Android Auto notifications

Extending a Notification for Android AutoEdit AndroidManifest.xml and include the following meta-data inside

the application tag:

<meta-data

android:name="com.google.android.gms.car.application"

android:resource="@xml/automotive_app_desc" />

code-help-2.txt

Page 11: Codelab - Android Auto notifications

Extending a Notification for Android AutoOpen MessagingService.java and add the code to create an

UnreadConversation object. Paste the following code (code-help-

3.txt) in the method sendNotificationForConversation(..), within the

delimiter comments.

This is where we type:

/// Add the code to create the UnreadConversation

/// End create UnreadConversation

code-help-3.txt

Page 12: Codelab - Android Auto notifications

Extending a Notification for Android AutoOpen MessagingService.java. Locate the line where it creates the

notification and extend it with a CarExtender

/// Extend the notification with CarExtender.

.extend(new

CarExtender().setUnreadConversation(unreadConversationBuilder.build())

)

/// End extend the notification with CarExtender

code-help-4.txt

Page 13: Codelab - Android Auto notifications

Run the new code

Click the Android Studio Run button (or press shift+F10).

The app looks the same, but now the notification created by it will

show up on Android Auto.

Page 14: Codelab - Android Auto notifications

Test on Android Auto DeviceRight here!

Page 15: Codelab - Android Auto notifications

Test on Android Auto SimulatorInstall the Android Messaging simulator

(<sdk>/extras/google/simulators/messaging-simulator.apk) on your

phone/tablet.

$ adb install messaging-simulator.apk

Enable the simulator to read notifications posted on the system:

Enable developer options.

Settings > Sounds & Notifications > Notification Access and check

the box labeled Messaging Simulator.

Page 16: Codelab - Android Auto notifications

Take Aways

● To be seen on Android Auto, notifications need to be extended

with a CarExtender

o this is in the support v4 library

● Extended notifications also show on the phone regardless of

whether Android Auto is connected or not, so you can support

Android Auto seamlessly without needing to know when the user

is connected to the car

● Android Auto only supports notifications from messaging apps.

This is enforced by the Play Store review process

Page 17: Codelab - Android Auto notifications

Congratulations

The messaging API is intentionally simple and builds on top of the

existing Android Notification API, requiring minimal effort from

developers.

Page 18: Codelab - Android Auto notifications

References

Developer.Android getting started with Android Auto

Android Auto & ubiquitous computing Udacity course

Making an Android Auto demo unit

Google IO code lab example

Android Auto notifications DevByte video

Android Auto help page