In this codelab, you will learn how to add Android Auto compatibility to a messaging app.

What you’ll learn

What you’ll need

How would you rate your experience with building Android apps?

NoviceIntermediateProficient

You can either download all the sample code to your computer... 

Download Zip

...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.

  1. Open Android Studo
  2. Select the androidauto-messaging/base directory from the code folder (File >  Import Project… > androidauto-messaging/base).
  3. Enable USB debugging and plug in your Android device.
  4. Click the Android Studio Run button (or press shift+F10).
  5. After a few seconds, you should see the app similar to the screenshot below.

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.

Frequently Asked Questions

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.

  1. Create a new file "res/xml/automotive_app_desc.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

  1. Replace the content of the file you just created with the following content. This declares that the application is using the Android Auto Messaging API.

automotive_app_desc.xml

<?xml version="1.0" encoding="utf-8"?>
<automotiveApp>
    <uses name="notification"/>
</automotiveApp>
  1. Edit AndroidManifest.xml and include the following meta-data inside the application tag:

AndroidManifest.xml

<meta-data
   android:name="com.google.android.gms.car.application"
   android:resource="@xml/automotive_app_desc" />
  1. Open MessagingService.java and add the necessary code to create an UnreadConversation object. Paste the following code in the method sendNotificationForConversation(..), within the delimiter comments. Read through the code now, which we'll explain in more depth later.

Before:

/// Add the code to create the UnreadConversation
/// End create UnreadConversation

After:

/// 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
  1. Open MessagingService.java. Locate the line where it creates the notification and extend it with a CarExtender:

        Before:

.setContentIntent(readPendingIntent)
/// Extend the notification with CarExtender.

/// End

After:

/// Extend the notification with CarExtender.
.extend(new CarExtender()
      .setUnreadConversation(unreadConversationBuilder.build()))
/// End
  1. Click the Android Studio Run button (or press shift+F10).
  2. The app looks the same, but now the notification created by it will show up on Android Auto. In the next step you will learn how to test it.

Takeaways

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!

What we've covered

Next Steps

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.