Google First Talked about Android Auto way back in June of 2014 and released first set of API’s for the same in November 2014.

The APIs available are for Calls, Music, Maps, and Messaging related features. You can extend features for the car in an optimized manner for your driving experience through mobile app development.

You can make mobile Apps for in vehicle Android Consoles or a hand held unit running Android. You really need not to worry about the hardware versions of either the car’s console or the handheld devices. Android Auto will take care of the same.

To enable your app for Auto, your App must target Android 5.0 (API level 21) or higher. Support for versions prior to that is not provided. Leveraging Android app development, Android Auto primarily handles most of the work to adjust each application’s user interface to the screen on which it is displayed. Your app’s manifest must declare the car capabilities that it uses, such as audio playback or messaging services.

For Handheld Consoles

A lot depends on the way the hand held consoles have integrated the Auto. Although not many in market yet, we expect the market to be flooded soon with the same. Costing anywhere between USD 80 to USD 300 in Indian market, they have started to gain popularity. Although the level of deep integration as compared to the In Car Console is bit less, however, it can take care of all your basic requirement of Maps and connecting to your phone and make your compatible Apps work on the console.

In Car Consoles

Almost every manufacturer is offering vehicles with in-built Android Auto in the new models of cars. Seems that eventually, it will be a default feature offered, especially in Cars.

How to make your mobile Apps Compatible for Android Auto?

Prerequisites

Declare the Auto Capabilities

The settings in your app manifest and a separate XML configuration file control the Auto features that your app can access. Before adding Auto features to your app, you must first define the Auto XML configuration file and add a manifest entry referencing your XML file.

Define the Auto XML configuration file

Specify the car capabilities that your app uses in an XML file that you place in your project’s resources directory (res/xml/). For example, to extend an audio application for Auto, create a file called automotive_app_desc.xml and store it under your project’s res/xml/ folder. The automotive_app_desc.xml file contains the following metadata:

<automotiveApp>
<uses name=”media” />
</automotiveApp>

The <uses> element declares the Auto capability your app intends to use. If your application uses multiple car capabilities, you can add multiple <uses>tags. The name attribute indicates the specific capability your app uses. The values supported are:

  • media – The app uses the Android framework APIs to play music in a vehicle. Set this value if you are enabling an audio app for Auto.
  • notification – The app displays message notifications in the car’s Home screen, allows users to select messages to be read aloud, and lets them respond through voice input. Set this value if you are enabling a messaging app for Auto.

Add a Manifest Entry

In your app’s manifest (AndroidManifest.xml), provide a reference to the Auto XML configuration file you created in the previous section. Add a”com.google.android.gms.car.application” metadata entry under the <application> element that references your Auto XML configuration file. Omit the .xml file extension when specifying the configuration filename.

The following code snippet shows how to include this reference in your manifest.
<application>

<meta-data android:name=”com.google.android.gms.car.application” android:resource=”@xml/automotive_app_desc”/>
</application>

Viola.. You are ready to add Auto Features to your App now. Before that, let’s look into Designing for Auto.

Android Auto Design

Auto does provide a standardized user interface and user interaction model. Android Auto does provide Auto UI guidelines just like their other platforms. Please note that there are certain design requirements that your App needs to meet in order to qualify as an Auto App on Google Play. Most of these requirements cater to driver distraction / safety.

The Design and quality requirements can be located at:

https://developer.android.com/develop/quality-guidelines/auto-app-quality.html

There are standard designs and layout provided for the design of Android Auto Apps:

  • Home Screen: It is the first screen users see when their device gets connected to the Car or handheld consoles. They are basically contextual cards based on parameters like location, time, new messages, etc.

  • Audio App Picker: User can tab on the Headphone icon in the activity bar to view all the audio compatible Apps in the device which the user can use. Your App will show up on this list.

  • Primary App UI: Once user selects the Audio App, the display shows primary App UI which can be customized to show App Name, Title Track name, Artist Name, etc..

  • User Actions: The media control card in the Primary App UI supports up to four main actions and 4 auxiliary actions in the overflow bar.

  • Drawer List: Used for browsing, drawer list can be used to display list view. The customized list UI shows the media containers and the audio files provided by the media service in your app.

  • Day and Night Transitions: All the UIs for Android Auto support different color schemes for day and night. All transitions are automatic.

Messaging Apps: Android framework allows the messaging Apps to extend their services into the driving experience of the user. It uses the standard user interface which allows the driver to keep their eyes on the road. If your App supports messaging, then it can extend the support of its notifications to the Android Auto.

Get Started With Audio Playback for Auto

Before thinking about targeting as many users as you can, let us become aware about the API level support for Android Auto. Well, audio services for Android Auto can work on devices with Android 5.0 or higher.

In order to provide audio content for Android Auto, let’s look into the following steps to begin with:

  • In order to make your app discoverable by Android Auto, you need to declare that your app can provide audio services for Android Auto
  • Create a service which can list out the browsable audio tracks available in your app
  • Define a service which can provide information regarding audio track listing
  • In order to provide playback controls, make use of the MediaSession object and its callbacks.

In order to make your app discoverable by Android Auto as an app providing audio services, just look back at Declare the Auto Capabilities section discussed earlier.

Now, as per the next step, let us go through the service we need to create in order to browse audio tracks.

<application>

<service
android:name=”.MyMusicService”
android:exported=”true”>
<intent-filter>
<action android:name=”android.media.browse.MediaBrowserService” />
</intent-filter>
</service>

<application>

Is your app connected?

It is important to determine whether your app is being used or not. Android Auto broadcasts an intent with com.google.android.gms.car.media.STATUS action when a user connects or disconnects from a media app.

IntentFilter filter = new IntentFilter(“com.google.android.gms.car.media.STATUS”);
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String status = intent.getStringExtra(“media_connection_status”);
boolean isConnectedToCar = “media_connected”.equals(status);
// adjust settings based on the connection status
}
};
registerReceiver(receiver, filter);

The broadcasted intent has a String extra media_connection_status, that contains either media_connected or media_disconnected string that represents the current connection status.

Creating a MediaBrowserService

Android Auto connects with your app using the implementation of the MediaBrowserService. You can create a media browser service by extending the MediaBrowserService class.

MediaBrowserService flow

1. In onCreate(), you need to create and register a MediaSession object and implement its callbacks.

@Override
public void onCreate() {
super.onCreate();
mSession = new MediaSessionCompat(this, “MyMusicService”);
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new MediaSessionCallback());
// To provide voice-enabled playback controls
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}

2. onGetRoot() method is called by Android Auto to fetch the parent node of the content hierarchy. This node is not displayed as a menu item. Infact, this node is used to fetch its child nodes displayed in the menu.
@Override
public BrowserRoot onGetRoot(String clientPackageName, int clientUid,
Bundle rootHints) {
// Verify that the specified package is allowed to access your
// content! You’ll need to write your own logic to do this.
if (!isValid(clientPackageName, clientUid)) {
// If the request comes from an untrusted package, return null.
// No further calls will be made to other media browsing methods.
return null;
}
return new BrowserRoot(MY_MEDIA_ROOT_ID, null);
}

3. Android Auto invokes onLoadChildren() method to fetch the children of the root node fetched through onGetRoot() method.

@Override
public void onLoadChildren(final String parentMediaId,
final Result<List<MediaItem>> result) {
// Assume for example that the music catalog is already loaded/cached.
List<MediaItem> mediaItems = new ArrayList<>();
// Check if this is the root menu:
if (MY_MEDIA_ROOT_ID.equals(parentMediaId)) {
// build the MediaItem objects for the top level,
// and put them in the mediaItems list
} else {
// examine the passed parentMediaId to see which submenu we’re at,
// and put the children of that menu in the mediaItems list
}
result.sendResult(mediaItems);
}

4. In case of submenu, onLoadChildren() is called again to retrieve the child nodes of the selected menu item.

5. Once the user begins playback, the respective media session callback method is called to perform that action.

Implementing Playback

The MediaSession.Callback is used when the user requests playback from your app. Let us look into some of the important callback methods :

onPlay() : This method is invoked in case user chooses to play an audio track without choosing a specific audio track. Thus, in this case, your app should play its default content.
onPlayFromMediaId() : This method is invoked when user chooses to play a specific audio track. The media ID of that specific item is passed to this method.
onPlayFromSearch() : This method is invoked when user chooses to play from a search query. The app should make an appropriate choice based on the passed search string.
onPause() : Pauses playback.
onSkipToNext() : Skips to the next item.
onSkipToPrevious() : Skips to the previous item.
onStop() : Stops playback.

Support Voice Actions

If your app provides voice action support, then it becomes very easy for the users to play/pause music through voice input thus enhancing user experience.

Enable audio playback requests

Add the following entry in your manifest in order to make sure that your app launches with a voice command such as Play [search query] on [your app name] :
<activity>
<intent-filter>
<action android:name=
“android.media.action.MEDIA_PLAY_FROM_SEARCH” />
<category android:name=
“android.intent.category.DEFAULT” />
</intent-filter>
</activity>

When the user says “Play music on [your app name]” on an Auto screen, Auto attempts to launch your app and play audio by calling your app’s MediaSession.Callback.onPlayFromSearch() method. If the user has not specified criteria such as a track name or music genre, the MediaSession.Callback.onPlayFromSearch() method receives an empty query parameter. Your app should respond by immediately playing audio, such as a song from a random queue or the most recent playlist.

Parse the voice request

As soon as a user requests for playback through voice input, the onPlayFromSearch() method is called with the voice search results and an extras bundle.

To parse the voice request and play audio accordingly, go through the following steps :

Use the search query string returned from the voice search to filter results.
Create the audio content queue based on these results.
Play the audio content.

You can use the following code to handle the search query and extras for playing audio content in your app :

@Override
public void onPlayFromSearch(String query, Bundle extras) {
if (TextUtils.isEmpty(query)) {
// The user provided generic string e.g. ‘Play music’
// Build appropriate playlist queue
} else {
// Build a queue based on songs that match “query” or “extras” param
String mediaFocus = extras.getString(MediaStore.EXTRA_MEDIA_FOCUS);
if (TextUtils.equals(mediaFocus,
MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE)) {
isArtistFocus = true;
artist = extras.getString(MediaStore.EXTRA_MEDIA_ARTIST);
} else if (TextUtils.equals(mediaFocus,
MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE)) {
isAlbumFocus = true;
album = extras.getString(MediaStore.EXTRA_MEDIA_ALBUM);
}
// Implement additional “extras” param filtering
}
// Implement your logic to retrieve the queue
if (isArtistFocus) {
result = searchMusicByArtist(artist);
} else if (isAlbumFocus) {
result = searchMusicByAlbum(album);
}
if (result == null) {
// No focus found, search by query for song title
result = searchMusicBySongTitle(query);
}
if (result != null && !result.isEmpty()) {
// Immediately start playing from the beginning of the search results
// Implement your logic to start playing music
playMusic(result);
} else {
// Handle no queue found. Stop playing if the app
// is currently playing a song
}
}

Get Started With Messaging for Auto

To make your app support messaging for Android Auto, the following changes need to be made in your Manifest :

<application>
<meta-data android:name=”com.google.android.gms.car.application”
android:resource=”@xml/automotive_app_desc” />
<application>
Add an xml file to the res/xml/ your app’s development project directory as automotive_app_desc.xml, with the following content:
<automotiveApp>
<uses name=”notification”/>
</automotiveApp>
Now, as you know, to send and receive messages, we need to declare intent filters as follows :
<receiver android:name=”.MyMessageReadReceiver”
android:exported=”false”>
<intent-filter>
<action android:name=”com.myapp.android.messagingservice.MY_ACTION_MESSAGE_READ”/>
</intent-filter>
</receiver>
<receiver android:name=”.MyMessageReplyReceiver”
android:exported=”false”>
<intent-filter>
<action android:name=”com.myapp.android.messagingservice.MY_ACTION_MESSAGE_REPLY”/>
</intent-filter>
</receiver>

After that, you can use notifications in order to notify the users of the new messages.
Yes, that’s it !!

You are now ready to make an Android Auto app that can provide Audio as well as Messaging services !!

Well, if you have any doubts, then you can refer the official Google doc for the same. Please share your reviews with us, we will be thankful and happy to have them.

I would kindly request you to share your thoughts with us regarding Google Auto App. If you are interested in read more from us then I would recommend to read CarPlay Compatible App Development published last week.

 

Want to work with us? We're hiring!