实现注意力分散预防措施
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
由于使用 Android Auto 时用户的手机会连接到汽车的扬声器,因此您必须采取额外的预防措施,防止驾驶员分心。
开发 Android Auto 媒体应用时,请实现特定的安全措施,以最大限度地减少驾驶员分心。这些保障措施包括:
为此,请使用 CarConnection
API 检测手机是否投屏到汽车屏幕。如果支持,则停用闹钟或提供用于管理闹钟的手机界面。
对于广告,请设置 METADATA_KEY_IS_ADVERTISEMENT
元数据键以禁止显示干扰性通知。
禁止车载闹钟
除非用户通过按下播放按钮等方式开始播放,否则 Android Auto 媒体应用不得通过汽车扬声器开始播放音频。即使是用户在您的媒体应用中设定的闹钟,也不得通过汽车扬声器开始播放音乐。
为了满足此要求,您的应用可以在播放任何音频之前使用 CarConnection
作为信号。应用可以检查手机是否正投影到车载显示屏。观察连接类型的 LiveData
。
确认该值等于 CONNECTION_TYPE_PROJECTION
。
如果用户的手机正在投影,支持闹钟的媒体应用必须执行以下任一操作:
默认情况下,当媒体元数据在音频播放会话期间发生更改时,Android Auto 会显示通知。当媒体应用从播放音乐切换到投放广告时,显示通知会导致用户分心。为阻止 Android Auto 显示通知,请将媒体元数据键 METADATA_KEY_IS_ADVERTISEMENT
设置为 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());
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-22。
[null,null,["最后更新时间 (UTC):2025-08-22。"],[],[],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 }"]]