如何检测 phone 是否连接到 Android Auto

How to detect if phone is connected to Android Auto

我正在制作一个应用程序,当 phone 连接到 Android 汽车时,它应该会改变其行为。它没有任何自动功能,也不会 marketed/submitted 作为 Android 自动应用程序。

有没有办法检测 phone 是否连接到 Android 汽车?我知道汽车媒体应用程序可以通过为 com.google.android.gms.car.media.STATUS 操作注册的 BroadcastReceiver 来检测它们的连接状态。 documentation 不是 100% 清楚,所以这是否也适用于所有其他非汽车应用程序?

编辑:使用 Android 12,解决方案不再有效,相反,最好使用 CarConnection API 记录 here

我知道这是一个旧话题,但因为它在 Google、here is the answer from another question

中排在第一位
public static boolean isCarUiMode(Context c) {
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR) {
    LogHelper.d(TAG, "Running in Car mode");
    return true;
} else {
    LogHelper.d(TAG, "Running on a non-Car mode");
    return false;
}

}