CarContext
public class CarContext extends ContextWrapper
| java.lang.Object | |||
| ↳ | android.content.Context | ||
| ↳ | android.content.ContextWrapper | ||
| ↳ | androidx.car.app.CarContext |
TestCarContext |
The |
The CarContext class is a ContextWrapper subclass accessible to your CarAppService and Screen instances, which provides access to car services such as the ScreenManager for managing the screen stack, the AppManager for general app-related functionality such as accessing a surface for drawing your navigation app's map, and the NavigationManager used by turn-by-turn navigation apps to communicate navigation metadata and other navigation-related events with the host. See Access the navigation templates for a comprehensive list of library functionality available to navigation apps.
Whenever you use a CarContext to load resources, the following configuration elements come from the car screen's configuration, and not the phone:
- Screen width.
- Screen height.
- Screen pixel density (DPI).
- Night mode (See
isDarkMode).
Please refer here, on how to use configuration qualifiers in your resources.
| See also | |
|---|---|
getCarService |
Summary
Constants |
|
|---|---|
static final String |
ACTION_NAVIGATE = "androidx.car.app.action.NAVIGATE"Standard action for navigating to a location. |
static final String |
APP_SERVICE = "app"Manages all app events such as invalidating the UI, showing a toast, etc. |
static final String |
CAR_SERVICE = "car"Internal usage only. |
static final String |
@RequiresCarApi(value = 2)Manages constraints for the app as enforced by the connected host. |
static final String |
EXTRA_START_CAR_APP_BINDER_KEY = "androidx.car.app.extra.START_CAR_APP_BINDER_KEY"Key for including a IStartCarApp in the notification |
static final String |
@RequiresCarApi(value = 3)Manages access to androidx.car.app.hardware properties, sensors and actions. |
static final String |
@ExperimentalCarApiManages the media requests from 3p apps such as providing a media session token, |
static final String |
NAVIGATION_SERVICE = "navigation"Manages all navigation events such as starting navigation when focus is granted, abandoning navigation when focus is lost, etc. |
static final String |
SCREEN_SERVICE = "screen"Manages the screens of the app, including the screen stack. |
static final String |
SUGGESTION_SERVICE = "suggestion"Manages posting suggestion events |
Public methods |
|
|---|---|
void |
Requests to finish the car app. |
@Nullable ComponentName |
@RequiresCarApi(value = 2)Return the component (service or activity) that invoked this car app. |
int |
Retrieves the API level negotiated with the host. |
@NonNull Object |
getCarService(@NonNull String name)Provides a car service by name. |
@NonNull T |
<T> getCarService(@NonNull Class<T> serviceClass)Returns a car service by class. |
@NonNull String |
getCarServiceName(@NonNull Class<Object> serviceClass)Gets the name of the car service that is represented by the specified class. |
@Nullable HostInfo |
Returns information about the host attached to this service. |
@NonNull OnBackPressedDispatcher |
Returns the |
boolean |
Returns |
void |
requestPermissions(Requests the provided |
void |
requestPermissions(Requests the provided |
void |
@RequiresCarApi(value = 2)Sets the result of this car app. |
void |
startCarApp(@NonNull Intent intent)Starts a car app on the car screen. |
static void |
This method is deprecated. use |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Constants
ACTION_NAVIGATE
public static final String ACTION_NAVIGATE = "androidx.car.app.action.NAVIGATE"
Standard action for navigating to a location.
Used as the Intent's action for starting a navigation via startCarApp.
APP_SERVICE
public static final String APP_SERVICE = "app"
Manages all app events such as invalidating the UI, showing a toast, etc.
CAR_SERVICE
public static final String CAR_SERVICE = "car"
Internal usage only. Top level binder to host.
CONSTRAINT_SERVICE
@RequiresCarApi(value = 2)
public static final String CONSTRAINT_SERVICE = "constraints"
Manages constraints for the app as enforced by the connected host.
EXTRA_START_CAR_APP_BINDER_KEY
public static final String EXTRA_START_CAR_APP_BINDER_KEY = "androidx.car.app.extra.START_CAR_APP_BINDER_KEY"
Key for including a IStartCarApp in the notification Intent, for starting the app if it has not been opened yet.
HARDWARE_SERVICE
@RequiresCarApi(value = 3)
public static final String HARDWARE_SERVICE = "hardware"
Manages access to androidx.car.app.hardware properties, sensors and actions.
MEDIA_PLAYBACK_SERVICE
@ExperimentalCarApi
public static final String MEDIA_PLAYBACK_SERVICE = "media_playback"
Manages the media requests from 3p apps such as providing a media session token,
NAVIGATION_SERVICE
public static final String NAVIGATION_SERVICE = "navigation"
Manages all navigation events such as starting navigation when focus is granted, abandoning navigation when focus is lost, etc.
SCREEN_SERVICE
public static final String SCREEN_SERVICE = "screen"
Manages the screens of the app, including the screen stack.
SUGGESTION_SERVICE
public static final String SUGGESTION_SERVICE = "suggestion"
Manages posting suggestion events
Public methods
finishCarApp
public void finishCarApp()
Requests to finish the car app.
Call this when your app is done and should be closed. The Session corresponding to this CarContext will become State.DESTROYED.
At some point after this call, the OS will destroy your CarAppService.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
getCallingComponent
@RequiresCarApi(value = 2)
public @Nullable ComponentName getCallingComponent()
Return the component (service or activity) that invoked this car app.
This is who the data in setCarAppResult will be sent to. You can use this information to validate that the recipient is allowed to receive the data.
Starting applications for result is not implemented in Android Auto, and this method will always return null for that platform.
| Returns | |
|---|---|
@Nullable ComponentName |
the |
getCarAppApiLevel
public int getCarAppApiLevel()
Retrieves the API level negotiated with the host.
API levels are used during client and host connection handshake to negotiate a common set of elements that both processes can understand. Different devices might have different host versions. Each of these hosts will support a range of API levels, as a way to provide backwards compatibility.
Applications can also provide forward compatibility, by declaring support for a getMinCarAppApiLevel lower than getLatestCarAppApiLevel. See getMinCarAppApiLevel for more details.
Clients must ensure no elements annotated with a RequiresCarApi value higher than returned by this method is used at runtime.
Refer to RequiresCarApi description for more details on how to implement forward compatibility.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Returns | |
|---|---|
int |
a value between |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if invoked before the connection handshake with the host has been completed (for example, before |
getCarService
public @NonNull Object getCarService(@NonNull String name)
Provides a car service by name.
The class of the returned object varies by the requested name.
Currently supported car services, and their respective classes, are:
- An
AppManagerfor communication between the app and the host. - A
NavigationManagerfor management of navigation updates. - A
ScreenManagerfor management ofScreens. - A
ConstraintManagerfor management of content limits. - A
CarHardwareManagerfor interacting with car hardware (e.g. sensors) data. - A
SuggestionManagerfor postingandroidx.car.app.suggestion.model.Suggestions.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Parameters | |
|---|---|
@NonNull String name |
The name of the car service requested. This should be one of |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if the service referred by |
java.lang.NullPointerException |
if |
getCarService
public @NonNull T <T> getCarService(@NonNull Class<T> serviceClass)
Returns a car service by class.
See getCarService for a list of the supported car services.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if |
java.lang.NullPointerException |
if |
getCarServiceName
public @NonNull String getCarServiceName(@NonNull Class<Object> serviceClass)
Gets the name of the car service that is represented by the specified class.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Returns | |
|---|---|
@NonNull String |
the car service name to use with |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.NullPointerException |
if |
| See also | |
|---|---|
getCarService |
getHostInfo
public @Nullable HostInfo getHostInfo()
Returns information about the host attached to this service.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| See also | |
|---|---|
HostInfo |
getOnBackPressedDispatcher
public @NonNull OnBackPressedDispatcher getOnBackPressedDispatcher()
Returns the OnBackPressedDispatcher that will be triggered when the user clicks a back button.
The default back press behavior is to call pop.
To override the default behavior, register a androidx.activity.OnBackPressedCallback via calling addCallback. Using the LifecycleOwner ensures that your callback is only registered while its Lifecycle is at least STARTED.
If there is a androidx.activity.OnBackPressedCallback that is added and enabled, and you'd like to remove the top Screen as a part of the callback, you MUST call pop in the callback. The default behavior is overridden when you have a callback enabled.
isDarkMode
public boolean isDarkMode()
Returns true if the car is set to dark mode.
Navigation applications must redraw their map with the proper dark colors when the host determines that conditions warrant it, as signaled by the value returned by this method.
Whenever the dark mode status changes, you will receive a call to onCarConfigurationChanged.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
requestPermissions
public void requestPermissions(
@NonNull List<String> permissions,
@NonNull OnRequestPermissionsListener listener
)
Requests the provided permissions from the user, calling the provided
listener in the main thread.
The app can define a branded background to the permission request through the carPermissionActivityLayout theme attribute, by declaring it in a theme and referencing the theme from the androidx.car.app.theme metadata.
In AndroidManifest.xml, under the application element corresponding to the car app:
<meta-data android:name="androidx.car.app.theme" android:resource="@style/CarAppTheme"/>
CarAppTheme style is defined as any other themes in a resource file:
<resources> <style name="CarAppTheme"> <item name="carPermissionActivityLayout">@layout/app_branded_background</item> </style> </resources>
The default behavior is to have no background behind the permission request.
| See also | |
|---|---|
requestPermissions |
= |
requestPermissions
public void requestPermissions(
@NonNull List<String> permissions,
@NonNull Executor executor,
@NonNull OnRequestPermissionsListener listener
)
Requests the provided permissions from the user.
When the result is available, the listener provided will be called using the Executor provided.
This method should be called using a androidx.car.app.model.ParkedOnlyOnClickListener.
If this method is called while the host deems it is unsafe (for example, when the user is driving), the permission(s) will not be requested from the user.
If the Session is destroyed before the user accepts or rejects the permissions, the callback will not be executed.
CarAppActivity are not allowed and may be rejected during app submission. See CarAppActivity for more details.
| Parameters | |
|---|---|
@NonNull List<String> permissions |
the runtime permissions to request from the user |
@NonNull Executor executor |
the executor that will be used for calling the |
@NonNull OnRequestPermissionsListener listener |
listener that will be notified when the user takes action on the permission request |
| Throws | |
|---|---|
java.lang.NullPointerException |
if any of |
setCarAppResult
@RequiresCarApi(value = 2)
public void setCarAppResult(int resultCode, @Nullable Intent data)
Sets the result of this car app.
In Android Automotive OS, this is equivalent to setResult. Call this to set the result that your CarAppActivity will return to its caller.
This method is not implemented in Android Auto.
| Parameters | |
|---|---|
int resultCode |
the result code to propagate back to the originating app, often |
@Nullable Intent data |
the data to propagate back to the originating app |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if the method is not supported in the current platform. |
startCarApp
public void startCarApp(@NonNull Intent intent)
Starts a car app on the car screen.
The target application will get the Intent via onCreateScreen or onNewIntent.
Supported Intents:
An
Intentto navigate.- The action must be
ACTION_NAVIGATE. - The data URI scheme must be either a latitude,longitude pair, or a + separated string query as follows:
- 1) "geo:12.345,14.8767" for a latitude, longitude pair.
- 2) "geo:0,0?q=123+Main+St,+Seattle,+WA+98101" for an address.
- 3) "geo:0,0?q=a+place+name" for a place to search for.
An
Intentto make a phone call.- The
Intentmust be created as defined here. An
Intentto start this app in the car.- The component name of the intent must be the one for the
CarAppServicethat contains thisCarContext. If the component name is for a different component, the method will throw aSecurityException.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Throws | |
|---|---|
java.lang.SecurityException |
if the app attempts to start a different app explicitly or does not have permissions for the requested action |
androidx.car.app.HostException |
if the remote call fails. For example, if the intent cannot be handled by the car host. |
java.lang.NullPointerException |
if |
public static voidstartCarApp(@NonNull Intent notificationIntent, @NonNull Intent appIntent)
Starts the car app on the car screen.
Use this method if the app has received a broadcast due to a notification action.
| Parameters | |
|---|---|
@NonNull Intent notificationIntent |
the |
@NonNull Intent appIntent |
the |
| Throws | |
|---|---|
java.security.InvalidParameterException |
if |
java.lang.NullPointerException |
if either |