处理错误
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-22。
[null,null,["最后更新时间 (UTC):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)."]]