Chapter 4

Developing for Android Auto

IN THIS CHAPTER

check Determining whether your car is compatible with Android Auto

check Using Google Play in auto apps

check Performing the development prerequisites

check Creating a basic app

Some of the things you can do in your car today are absolutely amazing! The limitations of a radio and CD player have vanished from newer cars. These days, cars offer a slew of ways to entertain you while you drive. You have music options from an inordinate number of sources, and the car can tell you about the weather, movies, or latest sports scores. It can even read to you from a book. Voice control makes the driving experience fuller and yet reduces the risks from inattentive driving.

Cars talk and can receive your requests for information or changes in configuration. They give you instructions for getting from point A to point B and tell you about road and weather conditions. Your car can even check on your house status before you get home. (One app enables you to turn the lights on and change the house temperature if you have the right setup in your home.)

In case talking to your car isn’t enough, you can also talk with Google Assistant or Alexa (for example, Ford makes Alexa available through an app). There are also safety apps, such as the one that will dial 911 automatically for you if the car gets into an accident. The possibilities for programming your car to perform exciting new tasks are nearly limitless. The point of this chapter is to help you grasp the possibilities.

To program your 64 Mustang to perform incredible tasks never before seen in such a car, however, your car must have Android capability available. Even the newest cars may not have this capability, so verify that before you begin development. You must also consider the potential trade-offs involved. Some upgrades really aren’t upgrades at all as far as the user is concerned, and you need to develop your app with that in mind.

Warning Your app must also consider the environment from a safety perspective. If your app distracts a driver at the wrong time, it could cause an accident. For example, some features in Ford cars are disabled during driving, such as getting movie times and locations. You can obtain this information when stopped and even set the GPS based on which movie theater you select, but once you start driving again, the feature becomes unavailable. Some places in the world also place significant limits on what an auto app can do, so you need to check local regulations.

After you consider the parameters of your app, you follow a process similar to all of the other apps in this book. First, you select an emulator; then you create the app itself in basic steps. This chapter won’t make you an Android Auto app programming guru, but it’ll get you started with a very basic app.

Checking Auto Compatibility

What sort of an app you build, when the user can interact with it, and how the app is presented are just a few of the questions you must ask yourself when working with Android on a car. The environment can actually vary depending on how a user configures accessory devices. For example, a Ford with Sync 3 (https://www.ford.com/technology/sync/) comes with these features:

  • Certain built-in apps that let you ask about weather, sports, and movie times at local theaters
  • Voice-activated apps (some are, some aren’t)
  • Apps that are available at all times and others that are available only when the car is stopped
  • Functionality that’s available from tablets when plugged into the USB port, such as searchable music (you can tell the car what you want and the car will find it on the tablet)
  • Sync3-compatible apps (such as Ford+Alexa, https://media.ford.com/content/fordmedia/fna/us/en/news/2017/01/04/alexa-car-ford-amazon-shop-search-home.html) after pairing a smartphone to the car
  • Extended Android Auto support after connecting a smartphone to the car through the USB port

You can’t make any assumptions about your app environment because this is just Ford using Sync 3. Every other vendor out there has its own idea of just how to make Android work with its cars. The Android Auto connection is becoming more popular, however, with vendors such as BMW joining in (see https://www.digitaltrends.com/android/bmw-making-wireless-android-auto-free-in-most-cars-in-2020/). If you follow car technology, you may be knowingly nodding your head at this point because Apple tried the same thing with CarPlay (https://www.apple.com/ios/carplay/), which still hasn’t standardized any car vendor’s offerings. Even so, the article at https://thedroidguy.com/android-auto-top-5-functionalities-1065927 offers five reasons that Android Auto is better than CarPlay. Make sure you pay attention to the Current Limitations section at the end of the article, which helps you understand precisely how much planning you need to do for your app. The smart developer will understand the app market and choose accordingly.

Tip Even if everything else is just right, however, the car user may choose not to use Android Auto because Android Auto can actually interfere with built-in features. For one thing, Android Auto may not work quite as planned unless you configure the car and the smartphone precisely (see the article at https://www.techradar.com/news/pros-and-cons-to-using-android-auto-on-a-2019-honda-passport for one example). The voice control is still available, but only for features on your smartphone rather than those built in to the car. It seems to be one or the other, but not both with some cars. For example, when creating an Android Auto setup on a Sync 3–equipped car, you gain all sorts of smartphone-typical voice-assisted information and entertainment sources, but you lose the ability to search your music database or set the car’s temperature using voice command. Even the GPS support becomes manual, which is a real pain when you’re on the road and suddenly have to switch destinations. So whether Android Auto is a perk or not depends on just how gadget-fixated the driver is. Some will choose the comfort of the built-in features.

However, your app may not necessarily depend specifically on Android Auto to work. The Ford+Alexa and other apps demonstrate that you can work with the car’s native ability, which means that you can let the user keep familiar functionality while still providing value-added services. It all depends on how you set up and configure your app.

Remember Support for Android apps (no matter what form they take) is a relatively new car feature. You can find a list of cars that currently offer some level of Android support at https://www.android.com/auto/compatibility/. This list simply shows the models of cars that potentially have Android support. A Ford Taurus may fall within the range of years that support Android, but if it doesn’t have Sync 3 installed, you don’t get the required support. The smartphone must also meet specific standards and some features. For example, FordPass Connect requires a certain model year and a smartphone that uses a compatible AT&T network (see https://owner.ford.com/fordpass/fordpass-sync-connect.html).

Choosing the Google Play Services

Android Auto (described at https://play.google.com/store/apps/details/Android_Auto_Maps_Media_Messaging_Voice?id=com.google.android.projection.gearhead) isn’t the end of the line when it comes to accessing specialized apps and online functionality. The Android Auto app does provide considerable functionality in the form of all those Google apps you’ve come to love, plus a few specifically designed for people who drive. Of course, it provides full Google Assistant functionality so that you interact with your smartphone and its capabilities in hands-free mode. If you have a voice activation button on your car’s steering wheel, you can click it to gain instant access to Google Assistant without moving your hands from the steering wheel to the screen.

In addition to native Android Auto functionality, you can also download apps from Google Play Services. Here are a few of the apps to choose from today:

  • Amazon Music (https://www.amazon.com/b/?node=13337243011): Allows access to all your Amazon Music offerings and all the usual Amazon Music features, such as Prime offerings. This particular app can work with your vehicle in a number of ways, including the use of built-in or add-on Alexa support for cars that have it. Essentially, you can ask for music, including music you have purchased through Amazon music, in a variety of ways depending on your mood.
  • NPR One (https://one.npr.org/): Tracks current local, regional, and national news and other items of interest and presents them in a commercial-free setting that only National Public Radio (NPR) can provide. The app lets you skip stories that don’t interest you.
  • OverDrive (https://app.overdrive.com/): Accesses the contents of more than 20,000 libraries. Although you shouldn’t read books or stream video when driving, streaming audiobooks can be a great way to travel. You can also interact vocally with your local participating library to place a book on hold or perform other library-related tasks.
  • Pandora (https://www.pandora.com/): Streams high-quality and personalized radio-type broadcasts. The app theoretically evolves to present streamed music based on your input. This is the app for people who don’t have time or energy to select individual songs from a playlist or audio library. The streaming capability now includes podcasts as well.
  • Scanner Radio (https://scannerradio.app/index.html): Scans the police and fire bands and presents you with live conversations about fires and other incidents. This type of app comes in handy when you need to be aware of potential hazards while driving as soon as they happen.
  • Spotify (https://www.spotify.com/us/): Makes both free and subscribed access to Spotify, the largest music streaming service in the world, available while you drive. Free access does come with ads.
  • Tidal (https://tidal.com/): Provides a high-quality listening experience from an audio library of 60+ million tracks (enough so that you’d never have to listen to the same track twice). According to the developer, this app currently works on more than 400 car types that support Android Auto.
  • Waze (https://www.waze.com/): Delivers real-time road information by relying on input from fellow drivers. You can also use this as a GPS to find the best route given current road conditions, rather than the usual methods of shortest distance or most efficient gas mileage. If you want to join a carpool, Waze can help here as well.

Remember Now that you have an idea of what’s available today, you have to consider what you might want to develop (remembering the caveats in the previous section). The instructions at https://developer.android.com/distribute/best-practices/launch/distribute-cars tell you about the submission requirements for your Android Auto app. Book 6, Chapter 1 tells you more about publishing an app in general, which you’ll find helpful in jumping through the required hoops. One thing to remember is that even though Google Play Store supports all sorts of host devices, cars generally support only smartphones today, so you need to target your app’s platform accordingly.

Considering Notification Limits

When you’re working with your smartphone or tablet, you can receive notifications at any time. Of course, you can set preferences not to receive notifications from certain apps, but generally, most people receive most notifications all the time. Distracted driving is a serious problem, and you won’t find a lack of laws regarding it, so any Android app you create for a car needs to observe certain restrictions when making notifications. A notification must be

  • Important enough to interrupt the driver: Some examples are the imminent failure of a car’s safety feature or the presence of a pedestrian standing in the way of the car. A friend request from Facebook isn’t important enough to interrupt the driver, and you should avoid notifying the driver.
  • Appropriate when driving: You can think about this restriction in two ways:
    • You can let the notification, such as an important text message, wait until the car is stopped. Most APIs make it possible to detect when the car has stopped at a stop light or for other reasons.
    • The driver doesn’t need to know about the notification until the driver exits the vehicle. Advertisements about car insurance fall into this category.
  • Sufficient priority: When presenting a notification, the car’s software may decide that your notification simply isn’t high enough priority to display when other notifications have a higher priority. The fact that the right front tire is losing pressure rapidly is definitely higher priority than the telephone call from Mom.
  • Not illegal to present: State laws are just now starting to catch up with technology. Such laws are very likely to become enforceable through car software at some point. Your notification might not appear because presenting it may become illegal based on the history of presenting such notifications causing distractions. A recent article by Rural Mutual Insurance at https://www.ruralmutual.com/resource/auto/hands-free-not-risk-free/ points out the risks of hands-free apps not being risk free. In short, any nonessential interaction you have with the driver may eventually become illegal.

Notifications also have limits in their manner of presentation. The notification that makes a sound or vibrates the device and then presents some sort of visual presentation on a smartphone or tablet may appear differently on a car. For example, some cars have a bar across the top of the display to present certain notifications. The textual notification display is the only presentation you receive. Here are some other notification types:

  • Image: This is a picture of a person or entity that is contacting the driver by text or call.
  • Icon: This is an indicator of the app that’s making the notification, and it’s sometimes paired with the image of the person calling, texting, or posting. For example, a notification of someone posting from Facebook may show both the image of the person making the post and the Facebook icon.
  • Text-to-Speech (TTS): Instead of presenting pure text, the text appears on the panel, and the car reads the text for the driver. Using this approach keeps the driver’s focus on the road rather than the panel. Because TTS relies on a computer reading the text, the text you choose should be understandable when read by a computer voice. Short, common words with few syllables work best.
  • Close icon: Notifications always come with some means of being dismissed without being read. If the driver is busy, the notification should automatically dismiss itself after a short time to keep from distracting the driver.
  • Sound: The notification sound is not a ringtone but a sound that is provided by the system (rather than by your smartphone or other paired device). The sound is short-lived and simply alerts the driver to the presence of an essential notification. You use sound only when the notification is high priority — something that affects the driver’s safety or alerts to driver to either car or environmental concerns.
  • Voice commands: Most newer cars provide voice commands to interact with notifications. Using voice commands allows the driver to deal with the notification without looking at the panel and while keeping both hands on the wheel. Any notification you create must react properly to voice commands.

Creating an Emulator

Before you can do much development, you need a car with Android Auto installed, or an emulator. This chapter focuses on using the emulator, but the following sections offer a quick guide to using your car as a development platform.

Configuring your car for development

You can’t move your car to your office, so you need a smartphone with enough functionality to perform the development and then plug it into the car’s USB port. You don’t enable developer mode in the car; instead, you enable it on your smartphone by using these steps:

  1. Open the Android Auto app on your smartphone.

    If you don’t have Android Auto installed, your smartphone won’t connect to the car, even if the car is Android Auto enabled. Normally, your smartphone will automatically detect the need to install Android Auto when you plug it into the car’s USB port.

  2. Choose About.
  3. Tap the gear icon in the upper-right corner.

    You see Android Auto details.

  4. Scroll to the bottom of the page.

    One of the entries shows the Android Auto version.

  5. Tap the version entry ten times.

    Your smartphone tells you that developer mode is enabled.

Tip If you encounter problems with this procedure, make sure that your car actually does support Android Auto, that you have the latest updates installed, and that the car “sees” the smartphone attached to the USB port (which is normally confirmed by a notification, with the smartphone serving as an audio source). Make sure that you have the latest version of Android Auto installed on the smartphone as well. When all else fails, contact the car vendor to determine whether some special handshake is required to make the setup work.

Defining an emulator

Most developers rely on an emulator to perform most of the development work for their Android Auto app. To get started, you need to configure the emulator using the sections that follow.

Install the SDK support

Before you can do anything else, you need to install the required SDK support, as described in the following steps.

  1. Choose Configure⇒  SDK Manager or Tools⇒  SDK Manager.

    You see the Settings for the New Projects dialog box.

  2. Select SDK Tools.

    The list of SDK Tools includes Android Auto API Simulators and Android Auto Desktop Head Unit Emulator, as shown in Figure 4-1.

  3. Select the Android Auto API Simulators and Android Auto Desktop Head Unit Emulator options (along with any other required updates) and then click OK.

    You see the Confirm Change dialog box, shown in Figure 4-2. Make sure both entries are there. The figure shows the versions of the emulators used in the book. If you use different emulator versions, you may see differences in outputs.

  4. Click OK.

    The Component Installer appears, and you see the various steps needed to install the software. The process usually goes quickly.

  5. Click Finish.

    The Component Installer closes, and you see either the Android Studio main display or the IDE.

    Screenshot of the Settings for the New Projects dialog box displaying the list of SDK Tools including Android Auto API Simulators and Android Auto
Desktop Head Unit Emulator.

    FIGURE 4-1: Android Emulator tools you need to work with Android Auto.

    Screenshot of the Confirm Change dialog box displaying different versions of the emulators used in the book.

    FIGURE 4-2: Verify that you’re installing the right features.

  6. Perform Steps 1 through 5, choosing SDK Platform instead of SDK Tools, to fully install Android 9.0 (Pie) on your system.

    If the entry reads Partially Installed, as the highlighted one shown in Figure 4-3 does, the emulator won’t set up correctly.

Screenshot of the Settings for the New Projects dialog box for choosing SDK Platform instead of SDK Tools, to fully install Android 9.0 (Pie) on your system.

FIGURE 4-3: Ensure that you have the full version of Android 9.0 (Pie) installed.

Using the Canary Channel for updates

Android Studio provides more than one channel for updates. The channel you choose determines the features you obtain for development, but newer features often come with stability problems. Normally, you want to use the Stable Channel, but developing for Android Auto requires you to work with the Canary Channel, which will mean additional stability issues. (You’ll need to restart Android Studio a lot more often, and strange things will happen when you run your code.) If you choose not to continue with Android Auto development, going back to the Stable Channel is a good idea. The following steps help you configure the Canary Channel.

  1. Choose Configure⇒  SDK Manager or Tools⇒  SDK Manager.
  2. Select Appearance & Behavior/System Settings/Updates.

    You see the settings shown in Figure 4-4.

  3. Choose Canary Channel from the Automatically Check Updates for drop-down list.

    You see a warning message.

  4. Click Check Now.

    You see an update dialog box like the one shown in Figure 4-5, outlining the various updates available to you.

    Screenshot of the Settings for the New Projects dialog box displaying the updates in the Appearance & Behavior/System Settings/Updates.

    FIGURE 4-4: Change the channel used for updates.

    Screenshot of the IDE and Plugin updates dialog box displaying the list of new features and bug fixes in the new Android Studio 4.0 Canary 9.

    FIGURE 4-5: Obtain the updates needed for this project.

  5. Click Download.

    Your browser opens, and you see a site from which you can download the Android Studio 4.0 Canary 9 update.

  6. Download the update for your platform.

    Make certain you get the Android Studio 4.0 Canary 9 update and not the Download 3.6 RC 3 update.

  7. Agree with the licensing statement after reading it; then download the Canary Channel copy of Android Studio.

    You receive an archive file appropriate for your platform, such as a .zip file for Windows users.

  8. Rename the Android Studio folder located in the archive that you downloaded to android-studio-4.0-canary.

    This name change allows you to keep your original version of Android Studio intact and run the new version side by side with the old version.

  9. Copy the entire folder used to host Android Studio.

    For example, on a Windows system, you'd copy the file to C:Program FilesAndroid. Mac users should drag the files to their Applications folder, and Linux users should copy the files to the /usr/local/ (for personal copies) or /opt/ (for shared copies) folders. You should now have two folders in that directory: Android Studio and android-studio-4.0-canary.

  10. Open the copy of studio64.exe found in the C:Program FilesAndroidandroid-studio-4.0-canaryin folder (or the equivalent for other platforms).

    When working with Linux, you look for studio.sh.

    Android Studio offers to import the settings from your previous installation, as shown in Figure 4-6.

    Screenshot of the Android Studio offering to import the settings from your previous installation.

    FIGURE 4-6: Obtain the updates needed for this project.

  11. Choose the correct location for the settings and then click OK.

    You see the Android Setup Wizard dialog box, shown in Figure 4-7. This wizard doesn't perform a full install; it instead performs an update install.

  12. Follow the prompts to complete the installation process.

    Android Studio starts.

Screenshot of the  Android Setup Wizard dialog box for completing the Canary Channel version setup process.

FIGURE 4-7: Complete the Canary Channel version setup process.

Tip Your platform provides a method for making the Canary Channel version of Android Studio available in the same manner as you access your normal version. Both copies appear as Android Studio, but the Stable Channel version uses a green icon, while the Canary Channel version uses a yellow icon.

Make the Android Studio configuration change

The Android Automotive emulator won’t appear in the list of available emulators until you make a required configuration change, as described in the following steps.

  1. Locate the studioFlags.xml file on your system.
    • Windows: C:Users<User Name>.AndroidStudio3.5configoptions
    • Linux: ~/.AndroidStudio3.5/config/options
    • Mac: ~/Library/Preferences/AndroidStudioPreview3.5/options
  2. Open the file in a pure text editor, preferably one that's designed to work with XML files, and make the addition shown in bold in the following code.

    You can open the file in Android Studio if desired.

    <application>
    <component name="StudioFlags">
    <option name="data">
    <map>
    <entry key="npw.templates.automotive"
    value="true" />
    <entry aid:table="cell" aid:crows="1" aid:ccols="1" key="gradle.ide.new.sync"
    value="false"/>
    <entry aid:table="cell" aid:crows="1" aid:ccols="1" key="gradle.ide.single.variant.sync"
    value="false"/>
    </map>
    </option>
    </component>
    </application>

  3. Save the file. Close and restart Android Studio.

    Upon restarting, the Android Studio starts a little more slowly as it makes required configuration changes.

Configuring the emulator

After you have the required SDK and Android Studio changes in place, you can finally create an emulator. The following steps show you how:

  1. Choose Configure⇒  AVD Manager or Tools⇒  AVD Manager.

    You see the Android Virtual Device Manager dialog box.

  2. Click + Create Virtual Device and choose the new Automotive category.

    The dialog box lists a minimum of two entries, as shown in Figure 4-8. The Automotive emulator format appears in more vehicles than the Polestar 2 emulator does. However, you need to choose the emulator that best fits the target for your app. The example in this chapter relies on the Automotive emulator.

  3. Highlight Automotive (1024p landscape) and click Next.

    You see the Select a System Image dialog box, shown in Figure 4-9.

    Screenshot of the Android Virtual Device Manager dialog box to choose a device definition highlighting the  Automotive 1024p landscape.

    FIGURE 4-8: Choose the emulator you want.

    Screenshot of the Select a System Image dialog box to click Download to download the automotive part of the SDK.

    FIGURE 4-9: As with any emulator, you need a system image.

  4. Click Download to download the automotive part of the SDK.

    You see the Component Installer dialog box.

  5. Click Finish.

    You return to the Select a System Image dialog box.

  6. Click Next.

    The Verify Configuration dialog box appears. Given that this emulator runs slowly on most systems, you can use the ideas from the “Tips for getting the emulators to run” sidebar of Chapter 3 of this minibook as a resource for improving performance.

  7. Click Finish.

    The emulator is finally ready to use.

Developing an Android Auto App

If you survived the grueling ordeal of creating an emulator, you’re finally ready to create a basic app. The following sections get you started.

Creating the Project

This section assumes that you’re using Android Studio Version 4.0 Canary 9. Older versions of Android Studio will likely not work. They definitely won’t interact properly with the emulator you configure in the “Creating an Emulator” section, earlier in this chapter. The following steps help you create a new Android Auto project:

  1. Choose Start New Android Project from the main Android Studio menu. Select the Automotive tab.

    You see the Select a Project Template dialog box, shown in Figure 4-10. It contains two template types:

    • Media service: Lets you create apps that play music, radio, audiobooks, and all sorts of other audio content. The template actually offers two levels of support. The first is for Android Auto, which is the level you should select if you want to offer an app in the near future. The second is for Android Automotive OS, which is still in development.
    • Messaging service: Provides the means for users to receive incoming notifications, read messages aloud using text to speech, and send replies via voice input in the car. This template supports only Android Auto.
    Screenshot of the Select a Project Template dialog box, containing two template types: Media service and Messaging service.

    FIGURE 4-10: Select an automotive project template.

  2. Highlight the Media Service template and click Next.

    You see the Configure Your Project dialog box.

    Technical Stuff Unlike most dialog boxes used for project configuration, this one includes a Use Legacy Android Support Libraries check box. Selecting this option could cause your app to fail because of the restrictions that using old libraries causes. However, using the legacy libraries will also make your resulting app more stable than using the Canary Channel libraries, so you have to consider this trade-off when putting your project together.

    This dialog box also specifies a minimum API level of API 28: Android 9.0 (Pie). Given the experimental nature of Android Auto, you should likely stick with this level.

  3. Type 05_04_01 in the Name field, type com.allmycode.p05_04_01 in the Package Name field, and then click Finish.

    Android Studio creates the project for you.

    Remember During this process, you might see a notification from your firewall that it needs permission to unblock some features of the OpenJDK Platform binary. You must allow access or you won’t have the required functionality in the project.

    The old version of Gradle won’t work with this project, so the next step is to update it.

  4. Open the gradle-wrapper.properties file and change the gradle-4.8-bin.zip entry to read gradle-6.1-rc-1-all.zip. Then click Sync Now.

    A download dialog box appears, the new version of Gradle downloads, and the sync process begins. The sync process will require more time than usual.

Viewing the project configuration

This project is more of a shell than a full-fledged offering when you create it. However, it demonstrates important principles in creating an Android Auto app, and you can definitely use it as a skeleton for your own projects. When you open the app, you see the layout shown in Figure 4-11.

Screenshot depicting the layout of the Parts of an Android Auto app, containing the automotive-specific portions of the app.

FIGURE 4-11: Parts of an Android Auto app.

The project consists of three modules:

  • automotive: Contains the automotive-specific portions of the app. Except for a unique manifest and some resources, this part of the project doesn't contain any written code files (just those used for testing and those that are automatically generated). It also contains resources specific to this part of the project, but it contains no layout files because the OS, not the app, draws the UI. The most important part of this module is the AndroidManifest.xml file, which contains the following <uses-feature> element required for automotive apps:

    <uses-feature
    android:name="android.hardware.type.automotive"
    android:required="true"/>

    Remember Automotive apps also start without an <activity> element. You can add an <activity> element with <intent-filter> entries, but the <intent-filter> must never have the "android.intent.action.MAIN" or "android.intent.category.LAUNCHER" attributes.

  • mobile: Provides an interface for a smartphone, which is currently the Hello World example that you see as a starting point for other smartphone apps in this book. There are no surprises here; the manifest, code files, layouts, and resources all look like those of a standard smartphone app. Your physical mobile device (not emulated) must have Android Auto installed and have access to a music source before the app will run on it.
  • shared: Implements the majority of the project's functional code, including MyMusicService, shown in Figure 4-11. This is where you look for insights into how the app created for the smartphone pairs with the service created for the car. Only the automotive portion of the project is fully functional at this point, so you run this service on the Automotive emulator that you create in the “Defining an emulator” section, earlier in this chapter.

    Remember The automotive and mobile modules link to the shared module through a build.gradle entry in each module's dependencies section: implementation project(path: ':shared'). This entry tells Gradle to add the shared module into the other two modules, so what you see in the shared module appears in both outputs. Note that Figure 4-11 shows a project-level build.gradle file, along with one build.gradle for each of the modules.

    In looking at AndroidManifest.xml for the shared module, you see a service entry similar to the one used for the Android Wear example in Chapter 2 of this minibook. You also see this interesting <meta-data> element:

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

    This entry references the following XML file:

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

    The combination of the two entries tells Android Auto that this is a media app and that it should appear in the media portion of the UI.

Remember The purpose of all these modules is to create an app bundle, which contains multiple APKs so that each device downloads the code required to make it work properly through Dynamic Delivery of modules (see https://developer.android.com/guide/app-bundle/dynamic-delivery for details).

Performing required configuration tasks

As with the Android Wear example in Chapter 2 of this minibook, the automotive portion of this example requires some reconfiguration on your part because it runs as a service rather than as an app. The following steps help you perform these required configuration tasks.

  1. Choose Run⇒  Edit Configurations.

    There are three Android App configurations, as shown in Figure 4-12, and you must choose the correct one or the app won’t run.

  2. Highlight the automotive entry.

    You see the run and debug options for the automotive module.

  3. Choose Nothing in the Launch field of the Launch Options group.
  4. Click OK.

    Android Studio makes the appropriate changes in the project configuration.

Screenshot of the Run/Debug Configurations dialog box for reconfiguring the correct android app (automotive).

FIGURE 4-12: Reconfiguring the correct app.

Touring the Media Service app

Because this example works with a service, you need to perform a few steps before starting it:

  1. Choose Run⇒  Select Device.

    You see a list of devices to choose from.

  2. Click Automotive (1024p landscape) API 28.

    Android Studio sets the device to use for running the app.

  3. Choose Run⇒  Run.

    You see a list of modules to run.

  4. Click Automotive.

    Gradle builds the app, the emulator starts, and you see the app loaded into the emulator. This process can require some amount of time to complete, so it’s important to be patient.

Once the app starts to run, you see the home page of the Media Service as shown in Figure 4-13. The only functional part is a drop-down list box where you can choose the music source, which defaults to Bluetooth Audio.

Illustration of the home page of the Media Service app, where you can choose an audio source to be played.

FIGURE 4-13: The app’s home page, where you can choose an audio source.

The project template doesn’t supply much functionality, so when you choose My Application as a source, you won’t see much. However, if you choose the Local Media source and click the Browse icon (which looks like a musical note), you see what your app output would look like if you added some functionality, as shown in Figure 4-14.

This part of the app is fully functional now despite the skeletal nature of this example project. Click Ringtones and then Calypso Steel, and you hear the associated ringtone over your computer speakers. When the ringtone plays, you see the usual controls associated with media, Skip Back, Play/Pause, and Skip Forward. When working with the ringtones, skipping forward selects the next ringtone and clicking back selects the previous ringtone. It’s interesting to play with this part of the app to see how your app should behave.

Click the Apps button (the grid icon) to see a list of apps installed in the emulator, as shown in Figure 4-15. You see that My Application is listed as one of the apps. However, if you click it, you find that it tells you no media is available. Likewise, clicking Bluetooth Audio tells you that the Bluetooth functionality is disconnected. However, clicking Settings does display a rudimentary Settings feature.

The Notifications button (the bell icon) does display a notifications screen, but the default screen simply tells you that there are no notifications. Clicking the little bar at the bottom of this screen takes you back to the previous screen in the emulator. As mentioned earlier, what you get from the template is an app skeleton, nothing more.

Illustration of the Media Service app, to choose the Local Media
source and click the Browse icon, displaying the app output after adding some functionality.

FIGURE 4-14: Seeing how the browse feature will likely work in a completed app.

Illustration of the Media Service app, displaying a list of apps installed in the emulator, with My Application listed as one of the apps.

FIGURE 4-15: Choosing one of the installed apps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
35.170.81.33