public final class Car
- Object
- Car
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 Forms, 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.
Methods
public static void setApplication(CarApplication app) | Registers the app’s in-car experience. |
public static CarApplication getApplication() | Returns the registered car application, or null if none has been set. |
public static boolean isCarConnected() | Returns true while a head unit (CarPlay / Android Auto) is currently connected. |
public static CarContext getCurrentContext() | Returns the context for the currently connected head unit, or null when none is connected. |
public static void addConnectionListener(CarConnectionListener l) | Registers a listener notified (on the EDT) when a head unit connects or disconnects. |
public static void removeConnectionListener(CarConnectionListener l) | Removes a previously registered connection listener. |
public static CarContext startSession(CarBridge bridge) | Framework/port entry point: begins an in-car session for the supplied bridge. |
public static void endSession() | Framework/port entry point: ends the current in-car session. |
Inherited methods
Method details
setApplication
public static void setApplication(CarApplication app)init()
is the natural place). Replacing a previously registered application is allowed.Parameters
appCarApplication- the car application, or null to clear
getApplication
public static CarApplication getApplication()Returns
CarApplication, or nullisCarConnected
public static boolean isCarConnected()Returns
getCurrentContext
public static CarContext getCurrentContext()Returns
CarContext, or nulladdConnectionListener
public static void addConnectionListener(CarConnectionListener l)Parameters
lCarConnectionListener- the listener to add
removeConnectionListener
public static void removeConnectionListener(CarConnectionListener l)Parameters
lCarConnectionListener- the listener to remove
startSession
public static CarContext startSession(CarBridge bridge)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
bridgeCarBridge- the platform rendering bridge for the connected head unit
Returns
CarContext (also retrievable via getCurrentContext())endSession
public static void endSession()