In this codelab, you will learn how to add Android Auto compatibility to a messaging app.
You can either download all the sample code to your computer...
...or clone the GitHub repository from the command line.
$ git clone https://github.com/googlesamples/io2015-codelabs.git
First, we will bootstrap with a very basic messaging app that simply creates a notification.
androidauto-messaging/basedirectory from the code folder (File > Import Project… >
There is nothing much to play with except clicking on the button and seeing the notification after 15 seconds. For now, ignore the message about plugging your phone to Android Auto. If you plug your phone into the car (or run the Desktop Headunit), you won't see the notification. That's because a notification needs to be extended in order to show up on the car's head unit. Let's learn how to do it in the next step.
Note: A typical messaging app would post a notification and let the user click on the notification and reply to the message. We instead kept the phone UI to the minimum, so you can focus on the Android Auto integration.
As you've seen in the previous step, not all notifications will show on an Android Auto head unit. To be seen on Android Auto, a notification needs to be related to a messaging app and be augmented with a CarExtender from support library v4. First, to be recognized as a messaging app for Android Auto, we need to make an explicit declaration in our AndroidManifest.xml.
Right click in the "res" folder and select File -> New -> Android Resource File
Then fill the dialog with the following info:
File name: automotive_app_desc
Resource type: XML
Root element: automotiveApp
<?xml version="1.0" encoding="utf-8"?> <automotiveApp> <uses name="notification"/> </automotiveApp>
<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" />
/// Add the code to create the UnreadConversation /// End create UnreadConversation
/// Add the code to create the UnreadConversation // Build a RemoteInput for receiving voice input in a Car Notification RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).build(); // Building a Pending Intent for the reply action to trigger PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(), conversationId, getMessageReplyIntent(conversationId), PendingIntent.FLAG_UPDATE_CURRENT); // Create the UnreadConversation and populate it with the participant name, // read and reply intents. UnreadConversation.Builder unreadConversationBuilder = new UnreadConversation.Builder(sender) .setLatestTimestamp(timestamp) .setReadPendingIntent(readPendingIntent) .setReplyAction(replyIntent, remoteInput); // Note: Add messages from oldest to newest to the UnreadConversation.Builder // Since we are sending a single message here we simply add the message. // In a real world application there could be multiple messages which should be ordered // and added from oldest to newest. unreadConversationBuilder.addMessage(message); /// End create UnreadConversation
.setContentIntent(readPendingIntent) /// Extend the notification with CarExtender. /// End
/// Extend the notification with CarExtender. .extend(new CarExtender() .setUnreadConversation(unreadConversationBuilder.build())) /// End
Run your application again and click the Start Conversation button. Unplug the phone from the computer and plug it into Android Auto.
You should see your notification. Click on the notification and then hit the blue mic image to reply. This is what you will see:
If you look closely at the source code, the UnreadConversation has a PendingIntent that handles replies. Whenever it gets a reply, it sends another message. You can keep having this very productive conversation for as long as you want :-)
The messaging API is intentionally simple and builds on top of the existing Android Notification API, requiring minimal effort from developers.
We hope you had fun conversations with John Doe and your other new friends!
If you are interested in learning more about Android Auto, take a look at the codelab "Playing music on cars and wearables", which will teach you how to adapt a music player for Android Auto and Android Wear with the same codebase.
Also, take a look at our messaging sample. It is a more comprehensive version of this codelab.