CarContext
class CarContext : ContextWrapper
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 |
|
|---|---|
const String! |
ACTION_NAVIGATE = "androidx.car.app.action.NAVIGATE"Standard action for navigating to a location. |
const String! |
APP_SERVICE = "app"Manages all app events such as invalidating the UI, showing a toast, etc. |
const String! |
CAR_SERVICE = "car"Internal usage only. |
const String! |
@RequiresCarApi(value = 2)Manages constraints for the app as enforced by the connected host. |
const String! |
EXTRA_START_CAR_APP_BINDER_KEY = "androidx.car.app.extra.START_CAR_APP_BINDER_KEY"Key for including a IStartCarApp in the notification |
const String! |
@RequiresCarApi(value = 3)Manages access to androidx.car.app.hardware properties, sensors and actions. |
const String! |
@ExperimentalCarApiManages the media requests from 3p apps such as providing a media session token, |
const String! |
NAVIGATION_SERVICE = "navigation"Manages all navigation events such as starting navigation when focus is granted, abandoning navigation when focus is lost, etc. |
const String! |
SCREEN_SERVICE = "screen"Manages the screens of the app, including the screen stack. |
const String! |
SUGGESTION_SERVICE = "suggestion"Manages posting suggestion events |
Public functions |
|
|---|---|
Unit |
Requests to finish the car app. |
ComponentName? |
@RequiresCarApi(value = 2)Return the component (service or activity) that invoked this car app. |
Int |
Retrieves the API level negotiated with the host. |
Any |
getCarService(name: String)Provides a car service by name. |
T |
<T> getCarService(serviceClass: Class<T!>)Returns a car service by class. |
String |
getCarServiceName(serviceClass: Class<Any!>)Gets the name of the car service that is represented by the specified class. |
HostInfo? |
Returns information about the host attached to this service. |
OnBackPressedDispatcher |
Returns the |
Boolean |
Returns |
Unit |
requestPermissions(Requests the provided |
Unit |
requestPermissions(Requests the provided |
Unit |
@RequiresCarApi(value = 2)Sets the result of this car app. |
Unit |
startCarApp(intent: Intent)Starts a car app on the car screen. |
java-static Unit |
This function is deprecated. use |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Constants
ACTION_NAVIGATE
const val ACTION_NAVIGATE = "androidx.car.app.action.NAVIGATE": String!
Standard action for navigating to a location.
Used as the Intent's action for starting a navigation via startCarApp.
APP_SERVICE
const val APP_SERVICE = "app": String!
Manages all app events such as invalidating the UI, showing a toast, etc.
CAR_SERVICE
const val CAR_SERVICE = "car": String!
Internal usage only. Top level binder to host.
CONSTRAINT_SERVICE
@RequiresCarApi(value = 2)
const val CONSTRAINT_SERVICE = "constraints": String!
Manages constraints for the app as enforced by the connected host.
EXTRA_START_CAR_APP_BINDER_KEY
const val EXTRA_START_CAR_APP_BINDER_KEY = "androidx.car.app.extra.START_CAR_APP_BINDER_KEY": String!
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)
const val HARDWARE_SERVICE = "hardware": String!
Manages access to androidx.car.app.hardware properties, sensors and actions.
MEDIA_PLAYBACK_SERVICE
@ExperimentalCarApi
const val MEDIA_PLAYBACK_SERVICE = "media_playback": String!
Manages the media requests from 3p apps such as providing a media session token,
NAVIGATION_SERVICE
const val NAVIGATION_SERVICE = "navigation": String!
Manages all navigation events such as starting navigation when focus is granted, abandoning navigation when focus is lost, etc.
SCREEN_SERVICE
const val SCREEN_SERVICE = "screen": String!
Manages the screens of the app, including the screen stack.
SUGGESTION_SERVICE
const val SUGGESTION_SERVICE = "suggestion": String!
Manages posting suggestion events
Public functions
finishCarApp
fun finishCarApp(): Unit
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)
fun getCallingComponent(): ComponentName?
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 | |
|---|---|
ComponentName? |
the |
getCarAppApiLevel
fun getCarAppApiLevel(): Int
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
fun getCarService(name: String): Any
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 | |
|---|---|
name: String |
The name of the car service requested. This should be one of |
| Returns | |
|---|---|
Any |
The car service instance |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if the service referred by |
java.lang.NullPointerException |
if |
getCarService
fun <T> getCarService(serviceClass: Class<T!>): T
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.
| Parameters | |
|---|---|
serviceClass: Class<T!> |
the class of the requested service |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if |
java.lang.NullPointerException |
if |
getCarServiceName
fun getCarServiceName(serviceClass: Class<Any!>): String
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 | |
|---|---|
String |
the car service name to use with |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.NullPointerException |
if |
| See also | |
|---|---|
getCarService |
getHostInfo
fun getHostInfo(): HostInfo?
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
fun getOnBackPressedDispatcher(): OnBackPressedDispatcher
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
fun isDarkMode(): Boolean
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
fun requestPermissions(
permissions: (Mutable)List<String!>,
listener: OnRequestPermissionsListener
): Unit
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
fun requestPermissions(
permissions: (Mutable)List<String!>,
executor: Executor,
listener: OnRequestPermissionsListener
): Unit
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 | |
|---|---|
permissions: (Mutable)List<String!> |
the runtime permissions to request from the user |
executor: Executor |
the executor that will be used for calling the |
listener: OnRequestPermissionsListener |
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)
fun setCarAppResult(resultCode: Int, data: Intent?): Unit
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 | |
|---|---|
resultCode: Int |
the result code to propagate back to the originating app, often |
data: Intent? |
the data to propagate back to the originating app |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if the method is not supported in the current platform. |
startCarApp
fun startCarApp(intent: Intent): Unit
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 |
java-static funstartCarApp(notificationIntent: Intent, appIntent: Intent): Unit
Starts the car app on the car screen.
Use this method if the app has received a broadcast due to a notification action.
| Parameters | |
|---|---|
notificationIntent: Intent |
the |
appIntent: Intent |
the |
| Throws | |
|---|---|
java.security.InvalidParameterException |
if |
java.lang.NullPointerException |
if either |