自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
监控状态
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
每当有新数据时,典型仪表板就会更新驾驶、呼叫和媒体信息。Android 提供了以下 API,使 OEM 的仪表板能够轻松接收最新状态。
驾驶状态
在导航过程中,系统会发送行车路线事件。文件 packages/services/Car/car-lib/src/android/car/cluster/renderer/NavigationRenderer.java
包含适用于仪表板中导航应用渲染程序的抽象方法。
在扩展的 InstrumentClusterRenderingService
中,这些方法如下:
public class MyClusterRenderingService extends
InstrumentClusterRenderingService {
...
@Override protected NavigationRenderer getNavigationRenderer() {
return new NavigationRenderer() {
@Override CarNavigationInstrumentCluster getNavigationProperties() {...}
@Override void onStartNavigation() {...}
@Override void onStopNavigation() {...}
@Override void onNextTurnChanged(int event, String road, int turnAngle,
int turnNumber, Bitmap image, int turnSide) {...}
@Override void onNextTurnDistanceChanged(int distanceMeters, int
timeSeconds) {...}
};
}
}
您可以向这些方法添加自定义操作(如呈现),以显示所需信息。
呼叫状态
如需监控呼叫状态,请按以下步骤操作:
- 扩展
android.telecom.InCallService
。
public class ClusterInCallService extends InCallService {
...
- 在
AndroidManifest.xml
中注册该服务。
<service android:name="com.android.car.cluster.sample.ClusterInCallService"
android:permission="android.permission.BIND_INCALL_SERVICE"
android:exported="false">
- 替换 onCallAdded 和 onCallRemoved。
public void onCallAdded(Call call) {
...
call.registerCallback(mPhoneCallback);
mPhoneCallback.onStateChanged(call, call.getState());
}
public void onCallRemoved(Call call) {
...
call.unregisterCallback(mPhoneCallback);
}
- 注册回调以接收呼叫状态更改事件 (
Call#registerCallback
)。
private static class PhoneCallback extends Callback {
...
public void onStateChanged(Call call, int state) {...}
}
- 使用内容提供程序提取联系信息:
ContactsContract.PhoneLookup, ContactsContract.Contacts#openContactPhotoInputStream
)。
如需了解呼叫状态监控示例代码,请参阅:
packages/services/Car/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/ClusterInCallService.java
如需了解内容提供程序示例代码,请参阅:
packages/services/Car/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/TelecomUtils.java
。
您可以将系统配置为在收到与 MediaMetadata(如专辑或歌曲名称或封面图片)的变更或与播放状态(缓冲、暂停、播放和停止)的变更相关的事件时更新媒体状态。如需更新媒体状态,请执行以下操作:
- 使用
MediaSessionManager
并获取主控制器 (#getActiveSessions(null)[0]
)。
- 注册回调 (
MediaController#Callback
)。
- 订阅活跃会话更改
MediaSessionManager#addOnActiveSessionsChangedListener(...)
。
如需了解详情,请参阅:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Monitor status\n\nA typical instrument cluster updates driving, call, and media information whenever new data\ncomes in. Android provides the following API to enable an OEM's instrument cluster to receive\nup-to-date status easily.\n\nDriving status\n--------------\n\nDriving direction events are sent while navigation is ongoing. The file\n`packages/services/Car/car-lib/src/android/car/cluster/renderer/NavigationRenderer.java`\ncontains abstract methods for a navigation app renderer in the instrument cluster.\n\nIn an extended `InstrumentClusterRenderingService`, these methods are as follows: \n\n```transact-sql\npublic class MyClusterRenderingService extends\nInstrumentClusterRenderingService {\n ...\n @Override protected NavigationRenderer getNavigationRenderer() {\n return new NavigationRenderer() {\n @Override CarNavigationInstrumentCluster getNavigationProperties() {...}\n @Override void onStartNavigation() {...}\n @Override void onStopNavigation() {...}\n @Override void onNextTurnChanged(int event, String road, int turnAngle,\n int turnNumber, Bitmap image, int turnSide) {...}\n @Override void onNextTurnDistanceChanged(int distanceMeters, int\n timeSeconds) {...}\n };\n }\n}\n```\n\nYou can add custom actions (such as rendering) to these methods to display desired\ninformation.\n\nCall status\n-----------\n\nTo monitor call status, use the following steps:\n\n1. Extend `android.telecom.InCallService`. \n\n ```gdscript\n public class ClusterInCallService extends InCallService {\n ...\n ```\n2. Register the service in `AndroidManifest.xml`. \n\n ```gdscript\n \u003cservice android:name=\"com.android.car.cluster.sample.ClusterInCallService\"\n android:permission=\"android.permission.BIND_INCALL_SERVICE\"\n android:exported=\"false\"\u003e\n ```\n3. Override onCallAdded and onCallRemoved. \n\n ```verilog\n public void onCallAdded(Call call) {\n ...\n call.registerCallback(mPhoneCallback);\n mPhoneCallback.onStateChanged(call, call.getState());\n }\n public void onCallRemoved(Call call) {\n ...\n call.unregisterCallback(mPhoneCallback);\n }\n ```\n4. Register callbacks to receive call status change events (`Call#registerCallback`). \n\n ```gdscript\n private static class PhoneCallback extends Callback {\n ...\n public void onStateChanged(Call call, int state) {...}\n }\n ```\n5. Use content providers to pull contact information: \n `ContactsContract.PhoneLookup, ContactsContract.Contacts#openContactPhotoInputStream`).\n\nFor call status monitor sample code, see: \n\n`packages/services/Car/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/ClusterInCallService.java`\n\nFor content provider sample code, see: \n\n`packages/services/Car/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/TelecomUtils.java`.\n\nMedia status\n------------\n\nYou can configure the system to update the media status when it receive events related to a\nchange in MediaMetadata (such as album or song title or cover image) or playback state (buffering,\npause, play, and stop). To update the media status:\n\n1. Use `MediaSessionManager` and get primary controller (`#getActiveSessions(null)[0]`).\n2. Register callbacks (`MediaController#Callback`).\n3. Subscribe to on active session changed `MediaSessionManager#addOnActiveSessionsChangedListener(...)`.\n\nFor details, see:\n\n- [packages/services/Car/+/android-8.1.0_r9/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/MediaStateMonitor.java](https://android.googlesource.com/platform/packages/services/Car/+/android-8.1.0_r9/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/MediaStateMonitor.java)\n- [packages/services/Car/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/InstrumentClusterController.java](https://android.googlesource.com/platform/packages/services/Car/+/android-8.1.0_r9/tests/InstrumentClusterRendererSample/src/com/android/car/cluster/sample/InstrumentClusterController.java)"]]