本節詳細介紹程式庫的不同功能,您可以利用這些功能為搜尋點 (POI) 應用程式實作各項功能。
在資訊清單中宣告類別支援
應用程式必須在 CarAppService
的意圖篩選器中宣告 androidx.car.app.category.POI
車用應用程式類別:
<application>
...
<service
...
android:name=".MyCarAppService"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService" />
<category android:name="androidx.car.app.category.POI"/>
</intent-filter>
</service>
...
<application>
存取地圖範本
應用程式可以存取 PlaceListMapTemplate
,其專為在主機算繪的地圖旁顯示搜尋點商店資訊所設計。
為存取此範本,應用程式必須在 AndroidManifest.xml
中宣告 androidx.car.app.MAP_TEMPLATES
權限,如下所示:
<uses-permission android:name="androidx.car.app.MAP_TEMPLATES"/>
重新整理 PlaceListMapTemplate 內容
您可以允許駕駛人在瀏覽 PlaceListMapTemplate
建構的地點清單時,輕觸按鈕以重新整理內容。導入 OnContentRefreshListener
介面的 onContentRefreshRequested
方法,並使用 PlaceListMapTemplate.Builder.setOnContentRefreshListener
來設定範本的事件監聽器,以啟用清單重新整理功能。
下列程式碼片段說明如何在範本中設定監聽器:
Kotlin
PlaceListMapTemplate.Builder() ... .setOnContentRefreshListener { // Execute any desired logic ... // Then call invalidate() so onGetTemplate() is called again invalidate() } .build()
Java
new PlaceListMapTemplate.Builder() ... .setOnContentRefreshListener(() -> { // Execute any desired logic ... // Then call invalidate() so onGetTemplate() is called again invalidate(); }) .build();
只有在監聽器含有值時,重新整理按鈕才會顯示在 PlaceListMapTemplate
的標題中。
當駕駛人按一下重新整理按鈕時,系統會呼叫 OnContentRefreshListener
實作的 onContentRefreshRequested
方法。在 onContentRefreshRequested
中呼叫 Screen.invalidate
方法。主機隨後會呼叫應用程式的 Screen.onGetTemplate
方法,以使用重新整理的內容擷取範本。如要進一步瞭解如何重新整理範本,請參閱「重新整理範本內容」。只要 onGetTemplate
傳回的下一個範本類型相同,系統就會將其計為重新整理,而不會計入範本配額。
透過應用程式動作與 Google 助理整合
應用程式動作可讓使用者透過 Google 助理以語音啟動及控制 Android 應用程式。您可以新增應用程式動作功能,將 Google 助理與搜尋點應用程式整合。應用程式的相關功能包含內建意圖 (BII) 和其執行要求。舉例來說,使用者可透過這類功能說出「Ok Google,在 <應用程式名稱> 中尋找路邊停車位」,接著該應用程式就會開啟並顯示相應的畫面。
限制
應用程式動作有下列限制:
整合步驟
在
AndroidManifest.xml
檔案的<application>
元素中加入下列<meta-data>
元素:<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
如要讓 Google 助理透過深層連結連至應用程式內容,您必須在
AndroidManifest.xml
檔案中加入<intent-filter>
元素。在 Android Auto 中,意圖篩選器與行動應用程式相同。
在 Android Automotive OS 執行要求中,應用程式動作是由
CarAppService
工作階段所觸發。如要允許工作階段觸發深層連結,您必須在AndroidManifest.xml
檔案的<activity>
元素中指定意圖篩選器。
<activity ... android:name="androidx.car.app.activity.CarAppActivity"> … <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="YOUR_SCHEME" android:host="YOUR_HOST" /> </intent-filter> </activity>
找出具備適用功能的應用程式專案,在該專案的
res/xml
目錄中建立shortcuts.xml
檔案:<?xml version="1.0" encoding="utf-8"?> <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Your Capability definitions will go here --> </shortcuts>
更新應用程式來處理工作階段的執行要求。
以下是
Session.onCreateScreen
和Session.onNewIntent
的意圖處理方式範例:onCreateScreen
Kotlin
@Override fun onCreateScreen(@NonNull intent: Intent): Screen { if (intent.getData() != null) { val uri: Uri = intent.getData() // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters ... } }
Java
@Override public Screen onCreateScreen(@NonNull Intent intent) { if (intent.getData() != null) { Uri uri = intent.getData(); // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters … } }
onNewIntent
Kotlin
@Override fun onNewIntent(@NonNull intent: Intent): Screen { if (intent.getData() != null) { val uri: Uri = intent.getData() // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters … } }
Java
@Override public void onNewIntent(@NonNull Intent intent) { if (intent.getData() != null) { Uri uri = intent.getData(); // uri = "YOUR_SCHEME://YOUR_HOST?name=Levis%20center" // Build your Templates with parsed uri parameters … } }
功能
應用程式動作支援下列功能:
停車
動作 ID:actions.intent.GET_PARKING_FACILITY
說明:取得停車設施資訊。可指定停車位類型、應用程式名稱和地點。
支援的語言代碼:en-US
參數:
parkingFacility.disambiguatingDescription
:停車位類型的描述元,例如「free」、「valet」或「street」。您可以使用這個參數篩選停車設施的類型。- 下列參數有助於判斷使用者所要求停車設施的地點。如果完全沒有這些參數,您應該傳回最靠近使用者目前所在地點的設施。
parkingFacility.name
:設施所在地點的名稱,例如「Mountain View」parkingFacility.address
:設施所在地點的地址,例如「123 Easy St, Mountain View, CA」parkingFacility.geo.latitude
:設施的緯度,例如「37.3861」parkingFacility.geo.longitude
:設施的經度,例如「-122.084」
capability
定義範例:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is a sample shortcuts.xml -->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<capability android:name="actions.intent.GET_PARKING_FACILITY">
<intent>
<url-template
android:value="YOUR_SCHEME://YOUR_HOST{?name,address,disambiguatingDescription,latitude,longitude}">
<!-- Eg. name = "Googleplex" -->
<parameter
android:name="parkingFacility.name"
android:key="name"/>
<!-- Eg. address = "1600 Amphitheatre Pkwy, Mountain View, CA 94043" -->
<parameter
android:name="parkingFacility.address"
android:key="address"/>
<!-- Eg. disambiguatingDescription = "valet" -->
<parameter
android:name="parkingFacility.disambiguatingDescription"
android:key="disambiguatingDescription"/>
<!-- Eg. latitude = "37.3861" -->
<parameter
android:name="parkingFacility.geo.latitude"
android:key="latitude"/>
<!-- Eg. longitude = "-122.084" -->
<parameter
android:name="parkingFacility.geo.longitude"
android:key="longitude"/>
</intent>
</capability>
</shortcuts>
充電
動作 ID:actions.intent.GET_CHARGING_STATION
說明:取得充電站資訊。可指定應用程式名稱或地點。
支援的語言代碼:en-US
參數:
chargingStation.disambiguatingDescription
:充電站類型的描述元,例如「free」或「paid」。您可以使用這個參數篩選充電站的類型。- 下列參數有助於判斷使用者所要求充電站的地點。如果完全沒有這些參數,您應該傳回最靠近使用者目前所在地點的充電站。
chargingStation.name
:充電站所在地點的名稱,例如「Mountain View」chargingStation.address
:充電站所在地點的地址,例如「123 Easy St, Mountain View, CA」chargingStation.geo.latitude
:充電站的緯度,例如「37.3861」chargingStation.geo.longitude
:充電站的經度,例如「-122.084」
capability
定義範例:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is a sample shortcuts.xml -->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<capability android:name="actions.intent.GET_CHARGING_STATION">
<intent>
<url-template
android:value="YOUR_SCHEME://YOUR_HOST{?name,address,latitude,longitude,type}">
<!-- Eg. name = "Googleplex" -->
<parameter
android:name="chargingStation.name"
android:key="name"/>
<!-- Eg. address = "1600 Amphitheatre Pkwy, Mountain View, CA 94043" -->
<parameter
android:name="chargingStation.address"
android:key="address"/>
<!-- Eg. latitude = "37.3861" -->
<parameter
android:name="chargingStation.geo.latitude"
android:key="latitude"/>
<!-- Eg. longitude = "-122.084" -->
<parameter
android:name="chargingStation.geo.longitude"
android:key="longitude"/>
</intent>
</capability>
</shortcuts>