Class 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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidRegisters a listener notified (on the EDT) when a head unit connects or disconnects.static voidFramework/port entry point: ends the current in-car session.static CarApplicationReturns the registered car application, or null if none has been set.static CarContextReturns the context for the currently connected head unit, or null when none is connected.static booleanReturns true while a head unit (CarPlay / Android Auto) is currently connected.static voidRemoves a previously registered connection listener.static voidRegisters the app's in-car experience.static CarContextstartSession(CarBridge bridge) Framework/port entry point: begins an in-car session for the supplied bridge.
-
Method Details
-
setApplication
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
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
Returns the context for the currently connected head unit, or null when none is connected.
Returns
the live
CarContext, or null -
addConnectionListener
Registers a listener notified (on the EDT) when a head unit connects or disconnects.
Parameters
l: the listener to add
-
removeConnectionListener
Removes a previously registered connection listener.
Parameters
l: the listener to remove
-
startSession
Framework/port entry point: begins an in-car session for the supplied bridge. The framework creates a
CarContext, asks the registeredCarApplicationfor 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 viagetCurrentContext()) -
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.
-