CarAppService
abstract class CarAppService : Service
| kotlin.Any | ||||
| ↳ | android.content.Context | |||
| ↳ | android.content.ContextWrapper | |||
| ↳ | android.app.Service | |||
| ↳ | androidx.car.app.CarAppService |
The base class for implementing a car app that runs in the car
Service Declaration
The app must extend theCarAppService to be bound by the car host. The service must also respond to Intent actions coming from the host, by adding an intent-filter to the service in the AndroidManifest.xml that handles the SERVICE_INTERFACE action. The app must also declare what category of application it is (e.g. CATEGORY_NAVIGATION_APP). For example:
<service android:name=".YourAppService" android:exported="true"> <intent-filter> <action android:name="androidx.car.app.CarAppService" /> <category android:name="androidx.car.app.category.NAVIGATION"/> </intent-filter> </service>
For a list of all the supported categories see Supported App Categories.
Accessing Location
When the app is running in the car display, the system will not consider it as being in the foreground, and hence it will be considered in the background for the purpose of retrieving location as described here.To reliably get location for your car app, we recommended that you use a foreground service. If you have a service other than your CarAppService that accesses location, run the service and your `CarAppService` in the same process. Also note that accessing location may become unreliable when the phone is in the battery saver mode.
Summary
Constants |
|
|---|---|
const String! |
@ExperimentalCarApiUsed in the app manifest to declare that this app supports calling. |
const String! |
CATEGORY_CHARGING_APP = "androidx.car.app.category.CHARGING"This property is deprecated. use |
const String! |
@RequiresCarApi(value = 6)Used to declare that this app supports cluster in the manifest. |
const String! |
@RequiresCarApi(value = 6)Used in the app manifest. |
const String! |
@RequiresCarApi(value = 8)Used to declare that this app is a media app in the manifest. |
const String! |
@ExperimentalCarApiUsed to declare that this app is a messaging app in the manifest. |
const String! |
CATEGORY_NAVIGATION_APP = "androidx.car.app.category.NAVIGATION"Used to declare that this app is a navigation app in the manifest. |
const String! |
CATEGORY_PARKING_APP = "androidx.car.app.category.PARKING"This property is deprecated. use |
const String! |
CATEGORY_POI_APP = "androidx.car.app.category.POI"Used in the app manifest. |
const String! |
@RequiresCarApi(value = 6)Used to declare that this app is a settings app in the manifest. |
const String! |
@RequiresCarApi(value = 7)Used to declare that this app is a weather app in the manifest. |
const String! |
SERVICE_INTERFACE = "androidx.car.app.CarAppService"The full qualified name of the |
Public constructors |
|---|
Public functions |
|
|---|---|
abstract HostValidator |
Returns the |
Unit |
@CallSuper |
Session? |
This function is deprecated. use |
HostInfo? |
Returns information about the host attached to this service. |
Session? |
getSession(sessionInfo: SessionInfo)Returns the |
IBinder |
Handles the host binding to this car app. |
Session |
Creates a new |
Session |
@RequiresCarApi(value = 6)Creates a new |
Unit |
|
Boolean |
Handles the host unbinding from this car app. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
CATEGORY_CALLING_APP
@ExperimentalCarApi
const val CATEGORY_CALLING_APP = "androidx.car.app.category.CALLING": String!
Used in the app manifest to declare that this app supports calling.
const val CATEGORY_CHARGING_APP = "androidx.car.app.category.CHARGING": String!
Used to declare that this app is a charging app in the manifest.
CATEGORY_FEATURE_CLUSTER
@RequiresCarApi(value = 6)
const val CATEGORY_FEATURE_CLUSTER = "androidx.car.app.category.FEATURE_CLUSTER": String!
Used to declare that this app supports cluster in the manifest.
CATEGORY_IOT_APP
@RequiresCarApi(value = 6)
const val CATEGORY_IOT_APP = "androidx.car.app.category.IOT": String!
Used in the app manifest. It declares that this app declares physical objects with sensors, that connect and exchange data with other devices and systems.
CATEGORY_MEDIA_APP
@RequiresCarApi(value = 8)
const val CATEGORY_MEDIA_APP = "androidx.car.app.category.MEDIA": String!
Used to declare that this app is a media app in the manifest.
CATEGORY_MESSAGING_APP
@ExperimentalCarApi
const val CATEGORY_MESSAGING_APP = "androidx.car.app.category.MESSAGING": String!
Used to declare that this app is a messaging app in the manifest.
This app can be used to send and receive short-form chat messages (IM/SMS).
CATEGORY_NAVIGATION_APP
const val CATEGORY_NAVIGATION_APP = "androidx.car.app.category.NAVIGATION": String!
Used to declare that this app is a navigation app in the manifest.
const val CATEGORY_PARKING_APP = "androidx.car.app.category.PARKING": String!
Used to declare that this app is a parking app in the manifest.
CATEGORY_POI_APP
const val CATEGORY_POI_APP = "androidx.car.app.category.POI": String!
Used in the app manifest. It declares that this app finds points of interests (POI).
CATEGORY_SETTINGS_APP
@RequiresCarApi(value = 6)
const val CATEGORY_SETTINGS_APP = "androidx.car.app.category.SETTINGS": String!
Used to declare that this app is a settings app in the manifest. This app can be used to provide screens corresponding to the settings page and/or any error resolution screens e.g. sign-in screen.
CATEGORY_WEATHER_APP
@RequiresCarApi(value = 7)
const val CATEGORY_WEATHER_APP = "androidx.car.app.category.WEATHER": String!
Used to declare that this app is a weather app in the manifest.
SERVICE_INTERFACE
const val SERVICE_INTERFACE = "androidx.car.app.CarAppService": String!
The full qualified name of the CarAppService class.
This is the same name that must be used to declare the action of the intent filter for the app's CarAppService in the app's manifest.
| See also | |
|---|---|
CarAppService |
Public constructors
Public functions
createHostValidator
abstract fun createHostValidator(): HostValidator
Returns the HostValidator this service will use to accept or reject host connections.
By default, the provided HostValidator.Builder would produce a validator that only accepts connections from hosts holding TEMPLATE_RENDERER_PERMISSION permission.
Application developers are expected to also allow connections from known hosts which don't hold the aforementioned permission (for example, Android Auto and Android Automotive OS hosts below API level 31), by allow-listing the signatures of those hosts.
Refer to androidx.car.app.R.array.hosts_allowlist_sample to obtain a list of package names and signatures that should be allow-listed by default.
It is also advised to allow connections from unknown hosts in debug builds to facilitate debugging and testing.
Below is an example of this method implementation:
@Override @NonNull public HostValidator createHostValidator() { if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR; } else { return new HostValidator.Builder(context) .addAllowedHosts(androidx.car.app.R.array.hosts_allowlist_sample) .build(); } }
dump
@CallSuper
fun dump(fd: FileDescriptor, writer: PrintWriter, args: Array<String!>?): Unit
fungetCurrentSession(): Session?
Returns the current Session for the main display if one exists, otherwise returns null.
getHostInfo
fun getHostInfo(): HostInfo?
Returns information about the host attached to this service.
| See also | |
|---|---|
HostInfo |
getSession
fun getSession(sessionInfo: SessionInfo): Session?
Returns the Session associated to the given SessionInfo, or null if one doesn't exist.
onBind
@CallSuper
fun onBind(intent: Intent): IBinder
Handles the host binding to this car app.
This method is final to ensure this car app's lifecycle is handled properly.
Use onCreateSession and onNewIntent instead to handle incoming Intents.
onCreateSession
fun onCreateSession(): Session
Creates a new Session for the application.
This method is invoked the first time the app is started, or if the previous Session instance has been destroyed and the system has not yet destroyed this service.
Once the method returns, onCreateScreen will be called on the Session returned.
Called by the system, do not call this method directly.
| See also | |
|---|---|
startCarApp |
onCreateSession
@RequiresCarApi(value = 6)
fun onCreateSession(sessionInfo: SessionInfo): Session
Creates a new Session.
This method is invoked once per app-supported physical display with a unique SessionInfo identifying the type of display. Support for displays is declared within the AndroidManifest.xml. This method can also be invoked if the previous instance has been destroyed (ie. due to memory pressure) and the system has not yet destroyed this service.
This method is called by the system and should not be called directly.
| See also | |
|---|---|
startCarApp |