# Class Car

java.lang.Object
com.codename1.car.Car

public final class Car extends Object

The static entry point for Apple CarPlay and Google Android Auto support. Register your `CarApplication` once (typically from your app's `init()`), observe connection state, and let the framework drive the rest:

    Car.setApplication(new MyCarApplication());
    Car.addConnectionListener(new CarConnectionListener() {
        public void carConnected(CarContext ctx)   { startLocationStream(); }
        public void carDisconnected()              { stopLocationStream(); }
    });

##### How it maps to the platforms

CarPlay and Android Auto are template-based: they do not render Codename One `Form`s, only the fixed `CarTemplate` catalog (list / grid / pane / message / navigation / now-playing). When a head unit connects, the platform port hands the framework a `com.codename1.car.spi.CarBridge`, the framework asks your `CarApplication` for a root `CarScreen`, and renders that screen's template onto the head unit. On the simulator and unsupported ports there is no bridge, so the API is an inert no-op and `isCarConnected()` returns false.

##### Zero cost when unused

Merely referencing this class makes the build inject the native plumbing (CarPlay scene + entitlement on iOS, the `androidx.car.app` dependency + `CarAppService` on Android). Apps that never touch `com.codename1.car` get none of it.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addConnectionListener(CarConnectionListener l)`
  Registers a listener notified (on the EDT) when a head unit connects or disconnects.
  `static void`
  `endSession()`
  Framework/port entry point: ends the current in-car session.
  `static CarApplication`
  `getApplication()`
  Returns the registered car application, or null if none has been set.
  `static CarContext`
  `getCurrentContext()`
  Returns the context for the currently connected head unit, or null when none is connected.
  `static boolean`
  `isCarConnected()`
  Returns true while a head unit (CarPlay / Android Auto) is currently connected.
  `static void`
  `removeConnectionListener(CarConnectionListener l)`
  Removes a previously registered connection listener.
  `static void`
  `setApplication(CarApplication app)`
  Registers the app's in-car experience.
  `static CarContext`
  `startSession(CarBridge bridge)`
  Framework/port entry point: begins an in-car session for the supplied bridge.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### setApplication

    public static void setApplication(CarApplication app)

    Registers the app's in-car experience. Call once, before a head unit connects (your `init()` is the natural place). Replacing a previously registered application is allowed.

    ###### Parameters

    - `app`: the car application, or null to clear

  - ### getApplication

    public static CarApplication getApplication()

    Returns the registered car application, or null if none has been set.

    ###### Returns

    the registered `CarApplication`, or null

  - ### isCarConnected

    public static boolean isCarConnected()

    Returns true while a head unit (CarPlay / Android Auto) is currently connected.

    ###### Returns

    true if a car is connected

  - ### getCurrentContext

    public static CarContext getCurrentContext()

    Returns the context for the currently connected head unit, or null when none is connected.

    ###### Returns

    the live `CarContext`, or null

  - ### addConnectionListener

    public static void addConnectionListener(CarConnectionListener l)

    Registers a listener notified (on the EDT) when a head unit connects or disconnects.

    ###### Parameters

    - `l`: the listener to add

  - ### removeConnectionListener

    public static void removeConnectionListener(CarConnectionListener l)

    Removes a previously registered connection listener.

    ###### Parameters

    - `l`: the listener to remove

  - ### startSession

    public static CarContext startSession(CarBridge bridge)

    Framework/port entry point: begins an in-car session for the supplied bridge. The framework creates a `CarContext`, asks the registered `CarApplication` for a root screen on the EDT, renders it, and fires the connection callbacks. Called by the platform port when a head unit connects.

    ###### Parameters

    - `bridge`: the platform rendering bridge for the connected head unit

    ###### Returns

    the new `CarContext` (also retrievable via `getCurrentContext()`)

  - ### endSession

    public static void endSession()
    Framework/port entry point: ends the current in-car session. Fires the disconnect callbacks and tears down the screen stack. Called by the platform port when the head unit disconnects.