Developing an Android Auto App: Parking & Navigation
What is Android Auto??
Google’s Android Auto platform isn’t its own operating system. It is an Android mobile application developed by Google. It is an extension of a connected Android smartphone to a compatible car that can display some apps, entertainment, and mirror messages on a car’s dashboard.
Android Auto is designed to help keep driver’s attention on the road rather than their smartphone. Functions that are supported by Android auto are navigation, music, telephony, send view SMS messages, voice recognition.
Android Auto is only available on devices with Android 6.0 or later. Android Auto connects to your vehicle over a wired USB connection, but it can also be done wirelessly. You’ll first need an Android smartphone using Android 11 or higher (or a Pixel/Samsung device on Android 9 and up), as well as a vehicle that supports wireless Android Auto.
When you’ll connect your phone to the car’s dashboard head unit, all your auto compatible apps are accessible on the head unit.
Android Auto support the following types of apps:
- Media apps
- Messaging apps
- Navigation, parking, and charging apps
Let’s try to build a parking app:
Declaring dependencies
Add the below dependency in your app-level build.gradle file.
implementation “androidx.car.app:app:1.0.0”
Configure your app’s manifest files
- Declare your CarAppService
The host connects to your app through your CarAppService
implementation. You declare this service in your manifest to allow the host to discover and connect to your app.
You also need to declare your app’s category in the category
element of your app’s intent filter.
Your app needs to declare either the androidx.car.app.category.PARKING
or androidx.car.app.category.CHARGING
in category
element in the intent filter of its CarAppService
.
- Specify the app name and icon
You need to specify an app name and icon that the host can use to represent your app in the system UI.
You can specify the app name and icon that is used to represent your app using label
and icon
elements of your CarAppService.
- Create your CarAppService and Session
Create the service which needs to extend the CarAppService
class.
Implement the CarAppService.onCreateSession
method, which returns the Session
instance.
- Access the map template
Apps can access the PlaceListMapTemplate
, a template that displays a map along with a list of places.
The map can display markers corresponding to the places in the list.
To get access to this template, your app needs to declare the androidx.car.app.MAP_TEMPLATES
permission in its AndroidManifest.xml
- Create your start screen
Create the screen by defining classes that extend the Screen
class and implementing the Screen.onGetTemplate
method, which returns the Template instance representing the state of the UI to display in the car screen.
- Implement screen navigation
The ScreenManager
class provides a screen stack that you can use to push screens that can be popped automatically when the user selects a back button in the car screen, or uses the hardware back button available in some cars.
The Action.BACK
object is a standard Action
that automatically invokes ScreenManager.pop
.
- Handle user input
Your app can respond to user input by passing the appropriate listeners to the models.
The onClickNavigate
method can then start the default navigation car app by using the CarContext.startCarApp
method.
Test your app for Android Auto
Testing your auto supported apps would be difficult so Google provides DHU(Desktop head Unit). The Desktop Head Unit (DHU) enables your development machine to emulate an Android Auto head unit, so that you can run and test Android Auto apps. The DHU runs on Windows, macOS, and Linux systems.
To get started with DHU:
- Install the Android Auto app on your android phone.
- Install DHU
- In order to install DHU, open SDK Manager and under Extras, install Android Auto Desktop Head Unit emulator.You can find it in <sdk>/extras/google/auto directory.
3. Running DHU
- Open Auto companion app in your phone and click on the menu icon. You will find only one option available i.e Help & feedback, to enable developer mode tap the header image 10 times. This happens for the first time you install the app.
- When developer mode is ‘on’ you see there are three more options available in the menu. Tap on “Start head unit server” to start the head unit server.
- To make a connection between DHU and head unit server running on our phone set ADB port, type
adb forward tcp:5277 tcp:5277
in the terminal. Don’t forget this step, otherwise it may show ADB server problem or connection failed error! - Start the DHU.
cd <sdk>/extras/google/auto/
On Linux or OSX:./desktop-head-unit
On Windows,desktop-head-unit.exe
You are ready to build your android auto apps and to test them.