Implement distraction safeguards
Stay organized with collections
Save and categorize content based on your preferences.
Because a user's phone is connected to a car's speakers when using Android
Auto, you must take additional precautions to prevent driver distraction.
When you develop Android Auto media apps, implement specific safeguards to
minimize driver distraction. These safeguards include:
Preventing your app from automatically playing audio through car speakers,
even for user-scheduled alarms.
Managing how Android Auto displays notifications when your app switches
between music and ads.
To achieve this, use the CarConnection
API to detect if a phone projects to a
car screen. If it does, disable alarms or provide an on-phone UI to manage them.
For ads, set the METADATA_KEY_IS_ADVERTISEMENT
metadata key to suppress
distracting notifications.
Suppress alarms in the car
Android Auto media apps must not start playing audio through the car speakers
unless the user starts playback by, for example, pressing a Play button.
Even a user-scheduled alarm from your media app must not start playing music
through the car speakers.
To fulfill this requirement, your app can use CarConnection
as a signal before playing any audio. Your app can check if the phone is
projecting to a car screen. Observe the LiveData
for the connection type.
Confirm the value is equal to CONNECTION_TYPE_PROJECTION
.
If the user's phone is projecting, media apps that support alarms must perform
one of these actions:
By default, Android Auto displays a notification when the media metadata changes
during an audio playback session. When a media app switches from playing music
to running an advertisement, displaying a notification distracts the user. To
prevent Android Auto from displaying a notification, set the media metadata key
METADATA_KEY_IS_ADVERTISEMENT
to METADATA_VALUE_ATTRIBUTE_PRESENT
:
Kotlin
import androidx.media.utils.MediaConstants
override fun onPlayFromMediaId(mediaId: String, extras: Bundle?) {
MediaMetadataCompat.Builder().apply {
if (isAd(mediaId)) {
putLong(
MediaConstants.METADATA_KEY_IS_ADVERTISEMENT,
MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT)
}
// ...add any other properties you normally would.
mediaSession.setMetadata(build())
}
}
Java
import androidx.media.utils.MediaConstants;
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
if (isAd(mediaId)) {
builder.putLong(
MediaConstants.METADATA_KEY_IS_ADVERTISEMENT,
MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT);
}
// ...add any other properties you normally would.
mediaSession.setMetadata(builder.build());
}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-08-12 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-12 UTC."],[],[],null,["# Implement distraction safeguards\n\nBecause a user's phone is connected to a car's speakers when using Android\nAuto, you must take additional precautions to prevent driver distraction.\n\nWhen you develop Android Auto media apps, implement specific safeguards to\nminimize driver distraction. These safeguards include:\n\n- Preventing your app from automatically playing audio through car speakers,\n even for user-scheduled alarms.\n\n- Managing how Android Auto displays notifications when your app switches\n between music and ads.\n\nTo achieve this, use the `CarConnection` API to detect if a phone projects to a\ncar screen. If it does, disable alarms or provide an on-phone UI to manage them.\nFor ads, set the `METADATA_KEY_IS_ADVERTISEMENT` metadata key to suppress\ndistracting notifications.\n\nSuppress alarms in the car\n--------------------------\n\nAndroid Auto media apps must not start playing audio through the car speakers\nunless the user starts playback by, for example, pressing a **Play** button.\nEven a user-scheduled alarm from your media app must not start playing music\nthrough the car speakers.\n\nTo fulfill this requirement, your app can use [`CarConnection`](/reference/androidx/car/app/connection/CarConnection)\nas a signal before playing any audio. Your app can check if the phone is\nprojecting to a car screen. Observe the `LiveData` for the [connection type](/reference/androidx/car/app/connection/CarConnection#getType()).\nConfirm the value is equal to [`CONNECTION_TYPE_PROJECTION`](/reference/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_PROJECTION()).\n\nIf the user's phone is projecting, media apps that support alarms must perform\none of these actions:\n\n- Disable the alarm.\n\n- Re-play the alarm [`STREAM_ALARM`](/reference/android/media/AudioManager#STREAM_ALARM) and provide a UI on the phone screen\n to disable the alarm.\n\nHandle media advertisements\n---------------------------\n\nBy default, Android Auto displays a notification when the media metadata changes\nduring an audio playback session. When a media app switches from playing music\nto running an advertisement, displaying a notification distracts the user. To\nprevent Android Auto from displaying a notification, set the media metadata key\n[`METADATA_KEY_IS_ADVERTISEMENT`](/reference/androidx/media/utils/MediaConstants#METADATA_KEY_IS_ADVERTISEMENT()) to [`METADATA_VALUE_ATTRIBUTE_PRESENT`](/reference/androidx/media/utils/MediaConstants#METADATA_VALUE_ATTRIBUTE_PRESENT()): \n\n### Kotlin\n\n import androidx.media.utils.MediaConstants\n\n override fun onPlayFromMediaId(mediaId: String, extras: Bundle?) {\n MediaMetadataCompat.Builder().apply {\n if (isAd(mediaId)) {\n putLong(\n MediaConstants.METADATA_KEY_IS_ADVERTISEMENT,\n MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT)\n }\n // ...add any other properties you normally would.\n mediaSession.setMetadata(build())\n }\n }\n\n### Java\n\n import androidx.media.utils.MediaConstants;\n\n @Override\n public void onPlayFromMediaId(String mediaId, Bundle extras) {\n MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();\n if (isAd(mediaId)) {\n builder.putLong(\n MediaConstants.METADATA_KEY_IS_ADVERTISEMENT,\n MediaConstants.METADATA_VALUE_ATTRIBUTE_PRESENT);\n }\n // ...add any other properties you normally would.\n mediaSession.setMetadata(builder.build());\n }"]]