A partire dal 27 marzo 2025, ti consigliamo di utilizzare android-latest-release
anziché aosp-main
per compilare e contribuire ad AOSP. Per ulteriori informazioni, vedi Modifiche ad AOSP.
Configurare un host AppCard
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Usa AppCardHost
per comunicare con le app che mostrano le schede di app.
Autorizzazioni
Qualsiasi app che utilizza un AppCardHost
deve disporre delle seguenti autorizzazioni:
android.permission.INTERACT_ACROSS_USERS_FULL
android.permission.QUERY_ALL_PACKAGES
- (Solo a partire dal livello API 34)
android.car.permission.BIND_APP_CARD_PROVIDER
Inizializza
Per inizializzare l'host, fornisci:
- Contesto
updateRate
Frequenza di aggiornamento di Scheda app in millisecondi.
fastUpdateRate
Frequenza di aggiornamento in millisecondi per i componenti taggati con
EnforceFastUpdateRate
responseExecutor
Un thread su cui vuoi ricevere risposte dalle app AppCard.
Design
Puoi interagire con un AppCardHost
in due modi:
OPPURE,
AppCardListener
Il componente AppCardListener
si registra con un AppCardHost
per ricevere aggiornamenti sulle comunicazioni dalle app AppCardHost
e AppCard.
Le funzioni di interfaccia di un AppCardListener
sono descritte qui.
fun onAppCardReceived(AppCardContainer)
Questa funzione viene attivata quando viene ricevuta un'app card da un'app. Viene inviata come AppCardContainer
che fornisce un'app card e un appIdentifier
per identificare l'app che ha inviato l'app card
fun onComponentReceived(AppCardComponentContainer)
Questa funzione viene attivata quando un componente AppCard viene ricevuto da un'app. Viene inviata come AppCardComponentContainer
, che fornisce un componente AppCards, Identifier
per identificare l'app che ha inviato l'AppCard e un ID stringa che rimanda all'AppCard a cui il componente è correlato.
fun onProviderRemoved(String, String?)
Questa funzione viene attivata quando un fornitore di schede di app è stato rimosso o disattivato. Utilizza questo metodo per ripulire eventuali AppCard attive correlate al nome del pacchetto e all'autorità del provider specificati.
Se l'autorità è {@code null}
, significa che è stato rimosso un intero pacchetto.
fun onProviderAdded(String, String?)
Questa funzione viene attivata quando un fornitore di schede di app è stato aggiunto o attivato.
Esempi di utilizzo. Utilizza questa funzione come attivatore per aggiornare tutte le schede di app disponibili in un selettore di schede di app. Se l'autorità è {@code null}
, è stato aggiunto un intero
pacchetto.
fun onPackageCommunicationError(appIdentifier, Throwable)
Questa funzione viene attivata quando AppCardHost
rileva un errore durante la comunicazione con un fornitore di AppCard.
Esempi di utilizzo. Utilizza questo metodo per mostrare all'utente che è stato rilevato un errore in un'AppCard selezionata.
API
fun refreshCompatibleapp()
Questo metodo deve essere chiamato ogni volta che l'attività che utilizza l'host viene ripresa in modo che l'host possa aggiornare il proprio elenco di app che supportano le schede di app.
fun destroy()
Chiama questo metodo quando un'attività che utilizza un host viene distrutta
in modo che l'host possa ripulire tutte le connessioni e i membri interni.
fun registerListener(AppCardListener)
Utilizzato per registrare un AppCardListener
.
fun unregisterListener(AppCardListener)
Utilizzato per annullare la registrazione di un AppCardListener
.
fun getAllAppCards(AppCardContext)
Chiama questo metodo per fornire a un AppCardListener
registrato tutte le schede dell'app fornite nel sistema con un determinato AppCardContext
che fornisce ai fornitori suggerimenti su come strutturare la loro scheda dell'app.
fun requestAppCard(AppCardContext, appIdentifier, String)
Chiama questo metodo per fornire a un AppCardListener
registrato un'AppCard specifica, dato un AppCardContext
che fornisce ai fornitori suggerimenti su come strutturare la loro AppCard.
fun notifyAppCardRemoved(appIdentifier, String)
Avvisa un fornitore di AppCard che la sua AppCard non è più attiva.
fun notifyAppCardInteraction(appIdentifier, String, String, String)
Avvisare un fornitore di AppCard che è stata eseguita un'interazione con la sua AppCard.
L'unica interazione supportata è un clic sul pulsante, indicato da un
AppCardMessageConstants.InteractionMessageConstants.MSG_INTERACTION_ON_CLICK
.
Consigliamo di creare un pulsante per una scheda dell'app con un ascoltatore onClick
che chiami questa funzione con appIdentifier
, l'ID scheda dell'app, l'ID componente e l'ID interazione.
fun sendAppCardContextUpdate(AppCardContext, appIdentifier, String)
Invia un aggiornamento AppCardContext
per una scheda dell'app specifica. Ad esempio, quando passi dalla modalità Parcheggia alla modalità Guida, utilizza questo metodo per inviare un aggiornamento AppCardContext
in cui isInteractable
è impostato su false
per ogni scheda App attiva.
Domande frequenti
Dove posso trovare implementazioni di esempio?
Host di esempio. Mostra tutte le AppCard disponibili nel sistema, insieme alle funzionalità di test.
DriverUI e Pano Manager. Pano Manager funge da selettore, mentre DriverUI funge da presentatore.
Quante AppCard può supportare ogni AppCardContentProvider
?
Un AppCardContentProvider
può supportare un numero infinito di schede di app.
Tuttavia, assicurati di bilanciare il numero di schede dell'app con un rendimento ridotto rispetto a un'esperienza utente positiva.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-06-26 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-06-26 UTC."],[],[],null,["# Configure an AppCard host\n\nUse `AppCardHost` to communicate with apps that show AppCards.\n\nPermissions\n-----------\n\nAny app that uses an `AppCardHost` must have the following permissions:\n\n- `android.permission.INTERACT_ACROSS_USERS_FULL`\n- `android.permission.QUERY_ALL_PACKAGES`\n- (In API level 34 and higher **only** ) `android.car.permission.BIND_APP_CARD_PROVIDER`\n\nInitialize\n----------\n\nTo initialize the host, provide:\n\n- Context\n- `updateRate` AppCard update rate in milliseconds.\n- `fastUpdateRate` Update rate in milliseconds for components tagged with `EnforceFastUpdateRate`\n- `responseExecutor` A thread on which you want to receive responses from AppCard apps.\n\nDesign\n------\n\nAn `AppCardHost` can be interacted with in one of two ways:\n\n- Register as an [`AppCardListener`](#appcardlistener)\n\nOR,\n\n- Interact with the [`AppCardHost` APIs](#api)\n\n### AppCardListener\n\nThe `AppCardListener` component registers itself with an `AppCardHost`\nto receive communication updates from the `AppCardHost` and AppCard apps.\nThe interface functions of an `AppCardListener` are detailed here.\n\n\u003cbr /\u003e\n\n`fun onAppCardReceived(AppCardContainer)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard has been received from an app. It's sent as an `AppCardContainer` which provides an AppCard and an `appIdentifier` to identify the app that sent the AppCard\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onComponentReceived(AppCardComponentContainer)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard component is received from an app. It's sent as an `AppCardComponentContainer`, which provides an AppCards component, `Identifier` to identify the app that sent the AppCard, and a string ID to point to the AppCard to which the component is related.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onProviderRemoved(String, String?)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard provider has been removed or disabled. Use this method to clean up any active AppCard related to the given package name and provider authority.\u003cbr /\u003e\n\nIf authority is `{@code null}`, then an entire package was removed.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onProviderAdded(String, String?)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when an AppCard provider has been added or enabled.\u003cbr /\u003e\n\n**Sample usage.** Use this function as a trigger to refresh all available\nAppCards in an AppCard picker. If authority is `{@code null}`, then an entire\npackage was added.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun onPackageCommunicationError(appIdentifier, Throwable)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis function is triggered when the `AppCardHost` encounters an error when communicating with an AppCard provider.\n\n\u003cbr /\u003e\n\n**Sample usage.** Use this method to show to the user that an AppCard they have\nselected has encountered an error.\n\n\u003cbr /\u003e\n\n### APIs\n\n\u003cbr /\u003e\n\n`fun refreshCompatibleapp()`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis method should be called whenever the activity that is using the host is resumed so that the host can refresh its list of apps that support AppCards.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun destroy()`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method when an activity that is using a host is destroyed so that the host can clean up all connections and internal members.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun registerListener(AppCardListener)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nUsed to register an `AppCardListener`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun unregisterListener(AppCardListener)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nUsed to unregister an `AppCardListener`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun getAllAppCards(AppCardContext)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method to supply a registered `AppCardListener` with all the AppCards provided in the system with a given `AppCardContext` that provides hints to the providers on how to structure their AppCard.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun requestAppCard(AppCardContext, appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nCall this method to supply a registered `AppCardListener` with a specific AppCard, given an `AppCardContext` that gives hints to the providers on how to structure their AppCard.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun notifyAppCardRemoved(appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nNotify an AppCard provider that its AppCard is no longer active.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun notifyAppCardInteraction(appIdentifier, String, String, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nNotify an AppCard provider that its AppCard has been interacted with. The only supported interaction is a button click, which is signified by a `AppCardMessageConstants.InteractionMessageConstants.MSG_INTERACTION_ON_CLICK`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWe recommend creating a button for an AppCard with an `onClick` listener that calls this function with `appIdentifier`, AppCard ID, component ID, and interaction ID.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n`fun sendAppCardContextUpdate(AppCardContext, appIdentifier, String)`\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nSend an `AppCardContext` update for a specific AppCard. For example, when shifting from Park mode to Drive mode, use this method to send an `AppCardContext` update in which `isInteractable` is set to `false` for each active AppCard.\n\n\u003cbr /\u003e\n\nFAQ\n---\n\n1. Where can I find sample implementations?\n\n - [Sample host](https://android.googlesource.com/platform/packages/apps/Car/libs/+/refs/tags/ub-automotive-master-20250418/car-app-card-host-lib/sample-host/). Shows all available AppCards in the system, along with\n testing capabilities.\n\n - DriverUI and [Pano manager](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/services/Car/car_product/distant_display/apps/CarDistantDisplayPanoManager/;l=1?q=CarDistantDisplayPanoManager&sq=&ss=android%2Fplatform%2Fsuperproject%2Fmain). Pano manager acts as the picker while\n DriverUI acts as the presenter.\n\n2. How many AppCards can each `AppCardContentProvider` support?\n\n An `AppCardContentProvider` can support an infinite number of AppCards.\n However, be sure to balance the number of AppCards with degraded performance\n versus a positive user experience."]]