處理錯誤
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android Auto 和 Android Automotive (AAOS) 會將播放狀態設為 STATE_ERROR
,並提供本地化的錯誤訊息給使用者。應用程式隨後就能向使用者顯示訊息。
如要解決錯誤,請提供含有 setErrorMessage
的錯誤訊息。
如需可用於設計錯誤訊息的錯誤代碼清單,請參閱 PlaybackStateCompat
,指出錯誤性質。如果使用者必須操作手機才能解決問題,請在錯誤訊息中加入這項指示。
錯誤訊息必須提供給使用者,並以使用者的語言代碼完成本地化。舉例來說,如果內容在使用者語言代碼適用的地區不可用,請使用 ERROR_CODE_NOT_AVAILABLE_IN_REGION
。
Kotlin
mediaSession.setPlaybackState(
PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_ERROR)
.setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))
// ...and any other setters.
.build())
Java
mediaSession.setPlaybackState(
new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_ERROR)
.setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))
// ...and any other setters.
.build())
如要進一步瞭解錯誤狀態,請參閱「使用媒體工作階段:狀態和錯誤」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-22 (世界標準時間)。
[null,null,["上次更新時間:2025-08-22 (世界標準時間)。"],[],[],null,["# Handle errors\n\nAndroid Auto and Android Automotive (AAOS) set the playback state to\n`STATE_ERROR` and provide a user-facing, localized error message. The apps can\nthen display the message to the user.\n\nTo address an error, you provide an error message with [`setErrorMessage`](/reference/android/support/v4/media/session/PlaybackStateCompat.Builder#setErrorMessage(int,%20java.lang.CharSequence)).\n\nSee [`PlaybackStateCompat`](/reference/android/support/v4/media/session/PlaybackStateCompat) for a list of the error codes you can use when\ndesigning the error message to indicate the nature of the error. If a user\nmust interact with their phone to resolve an issue, include this instruction\nin the error message.\n\nError messages must be user-facing and targeted to the user's locale. For\nexample, if content is not available in the user's locale, use\n[`ERROR_CODE_NOT_AVAILABLE_IN_REGION`](/reference/android/support/v4/media/session/PlaybackStateCompat#ERROR_CODE_NOT_AVAILABLE_IN_REGION()). \n\n### Kotlin\n\n mediaSession.setPlaybackState(\n PlaybackStateCompat.Builder()\n .setState(PlaybackStateCompat.STATE_ERROR)\n .setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))\n // ...and any other setters.\n .build())\n\n### Java\n\n mediaSession.setPlaybackState(\n new PlaybackStateCompat.Builder()\n .setState(PlaybackStateCompat.STATE_ERROR)\n .setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))\n // ...and any other setters.\n .build())\n\nTo learn more about error states, see\n[Using a media session: States and errors](/guide/topics/media-apps/working-with-a-media-session#errors)."]]