實作預防分心駕駛的保護措施
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-22 (世界標準時間)。
[null,null,["上次更新時間: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 }"]]