Chapter 4
IN THIS CHAPTER
Determining whether your car is compatible with Android Auto
Using Google Play in auto apps
Performing the development prerequisites
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.
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.
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:
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 carYou 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.
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.
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:
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.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.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.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.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.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.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.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.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
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:
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.
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:
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.
Tap the gear icon in the upper-right corner.
You see Android Auto details.
Scroll to the bottom of the page.
One of the entries shows the Android Auto version.
Tap the version entry ten times.
Your smartphone tells you that developer mode is enabled.
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.
Before you can do anything else, you need to install the required SDK support, as described in the following steps.
Choose Configure⇒ SDK Manager or Tools⇒ SDK Manager.
You see the Settings for the New Projects dialog box.
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.
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.
Click OK.
The Component Installer appears, and you see the various steps needed to install the software. The process usually goes quickly.
Click Finish.
The Component Installer closes, and you see either the Android Studio main display or the IDE.
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.
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.
Select Appearance & Behavior/System Settings/Updates.
You see the settings shown in Figure 4-4.
Choose Canary Channel from the Automatically Check Updates for drop-down list.
You see a warning message.
Click Check Now.
You see an update dialog box like the one shown in Figure 4-5, outlining the various updates available to you.
Click Download.
Your browser opens, and you see a site from which you can download the Android Studio 4.0 Canary 9 update.
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.
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.
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.
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
.
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.
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.
Follow the prompts to complete the installation process.
Android Studio starts.
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.
studioFlags.xml
file on your system. C:Users
<User Name>
.AndroidStudio3.5configoptions
~/.AndroidStudio3.5/config/options
~/Library/Preferences/AndroidStudioPreview3.5/options
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>
Save the file. Close and restart Android Studio.
Upon restarting, the Android Studio starts a little more slowly as it makes required configuration changes.
After you have the required SDK and Android Studio changes in place, you can finally create an emulator. The following steps show you how:
Choose Configure⇒ AVD Manager or Tools⇒ AVD Manager.
You see the Android Virtual Device Manager dialog box.
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.
Highlight Automotive (1024p landscape) and click Next.
You see the Select a System Image dialog box, shown in Figure 4-9.
Click Download to download the automotive part of the SDK.
You see the Component Installer dialog box.
Click Finish.
You return to the Select a System Image dialog box.
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.
Click Finish.
The emulator is finally ready to use.
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.
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:
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:
Highlight the Media Service template and click Next.
You see the Configure Your Project dialog box.
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.
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.
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.
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.
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.
The project consists of three modules:
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"/>
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.
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.
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.
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.
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.
Highlight the automotive entry.
You see the run and debug options for the automotive module.
Click OK.
Android Studio makes the appropriate changes in the project configuration.
Because this example works with a service, you need to perform a few steps before starting it:
Choose Run⇒ Select Device.
You see a list of devices to choose from.
Click Automotive (1024p landscape) API 28.
Android Studio sets the device to use for running the app.
Choose Run⇒ Run.
You see a list of modules to run.
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.
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.
35.170.81.33