CarAppService
public abstract class CarAppService extends Service
| java.lang.Object | ||||
| ↳ | 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 |
|
|---|---|
static final String |
@ExperimentalCarApiUsed in the app manifest to declare that this app supports calling. |
static final String |
CATEGORY_CHARGING_APP = "androidx.car.app.category.CHARGING"This field is deprecated. use |
static final String |
@RequiresCarApi(value = 6)Used to declare that this app supports cluster in the manifest. |
static final String |
@ExperimentalCarApiUsed in the app manifest. |
static final String |
@ExperimentalCarApiUsed to declare that this app is a media app in the manifest. |
static final String |
@ExperimentalCarApiUsed to declare that this app is a messaging app in the manifest. |
static final String |
CATEGORY_NAVIGATION_APP = "androidx.car.app.category.NAVIGATION"Used to declare that this app is a navigation app in the manifest. |
static final String |
CATEGORY_PARKING_APP = "androidx.car.app.category.PARKING"This field is deprecated. use |
static final String |
CATEGORY_POI_APP = "androidx.car.app.category.POI"Used in the app manifest. |
static final String |
@ExperimentalCarApiUsed to declare that this app is a settings app in the manifest. |
static final String |
@ExperimentalCarApiUsed to declare that this app is a weather app in the manifest. |
static final String |
SERVICE_INTERFACE = "androidx.car.app.CarAppService"The full qualified name of the |
Public constructors |
|---|
Public methods |
|
|---|---|
abstract @NonNull HostValidator |
Returns the |
final void |
@CallSuper |
final @Nullable Session |
This method is deprecated. use |
final @Nullable HostInfo |
Returns information about the host attached to this service. |
final @Nullable Session |
getSession(@NonNull SessionInfo sessionInfo)Returns the |
final @NonNull IBinder |
Handles the host binding to this car app. |
@NonNull Session |
Creates a new |
@NonNull Session |
@RequiresCarApi(value = 6)Creates a new |
void |
|
final boolean |
Handles the host unbinding from this car app. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
CATEGORY_CALLING_APP
@ExperimentalCarApi
public static final String CATEGORY_CALLING_APP = "androidx.car.app.category.CALLING"
Used in the app manifest to declare that this app supports calling.
public static final String CATEGORY_CHARGING_APP = "androidx.car.app.category.CHARGING"
Used to declare that this app is a charging app in the manifest.
CATEGORY_FEATURE_CLUSTER
@RequiresCarApi(value = 6)
public static final String CATEGORY_FEATURE_CLUSTER = "androidx.car.app.category.FEATURE_CLUSTER"
Used to declare that this app supports cluster in the manifest.
CATEGORY_IOT_APP
@ExperimentalCarApi
public static final String CATEGORY_IOT_APP = "androidx.car.app.category.IOT"
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
@ExperimentalCarApi
public static final String CATEGORY_MEDIA_APP = "androidx.car.app.category.MEDIA"
Used to declare that this app is a media app in the manifest.
CATEGORY_MESSAGING_APP
@ExperimentalCarApi
public static final String CATEGORY_MESSAGING_APP = "androidx.car.app.category.MESSAGING"
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
public static final String CATEGORY_NAVIGATION_APP = "androidx.car.app.category.NAVIGATION"
Used to declare that this app is a navigation app in the manifest.
public static final String CATEGORY_PARKING_APP = "androidx.car.app.category.PARKING"
Used to declare that this app is a parking app in the manifest.
CATEGORY_POI_APP
public static final String CATEGORY_POI_APP = "androidx.car.app.category.POI"
Used in the app manifest. It declares that this app finds points of interests (POI).
CATEGORY_SETTINGS_APP
@ExperimentalCarApi
public static final String CATEGORY_SETTINGS_APP = "androidx.car.app.category.SETTINGS"
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
@ExperimentalCarApi
public static final String CATEGORY_WEATHER_APP = "androidx.car.app.category.WEATHER"
Used to declare that this app is a weather app in the manifest.
SERVICE_INTERFACE
public static final String SERVICE_INTERFACE = "androidx.car.app.CarAppService"
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 methods
createHostValidator
public abstract @NonNull HostValidator createHostValidator()
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
public final void dump(
@NonNull FileDescriptor fd,
@NonNull PrintWriter writer,
@Nullable String[] args
)
public final @Nullable SessiongetCurrentSession()
Returns the current Session for the main display if one exists, otherwise returns null.
getHostInfo
public final @Nullable HostInfo getHostInfo()
Returns information about the host attached to this service.
| See also | |
|---|---|
HostInfo |
getSession
public final @Nullable Session getSession(@NonNull SessionInfo sessionInfo)
Returns the Session associated to the given SessionInfo, or null if one doesn't exist.
onBind
@CallSuper
public final @NonNull IBinder onBind(@NonNull Intent intent)
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
public @NonNull Session onCreateSession()
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)
public @NonNull Session onCreateSession(@NonNull SessionInfo sessionInfo)
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 |