我想使用 Android Auto 在汽车主机上显示地图、窗格和一些图标。以下 Kotlin 代码运行没有错误。它在黑色背景上显示窗格和图标,但没有地图。我缺少什么?使用 androidx.car.app:app:1.7.0-beta03。我已经在模拟器(dhu)和汽车中进行了测试。
package se.ndssoft.autoglucose
import android.content.Intent
import androidx.car.app.CarAppService
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.Session
import androidx.car.app.annotations.ExperimentalCarApi
import androidx.car.app.model.Action
import androidx.car.app.model.ActionStrip
import androidx.car.app.model.Header
import androidx.car.app.model.Pane
import androidx.car.app.model.PaneTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.Template
import androidx.car.app.navigation.model.MapController
import androidx.car.app.navigation.model.MapWithContentTemplate
import androidx.car.app.validation.HostValidator
class CarHomeService : CarAppService() {
override fun createHostValidator(): HostValidator {
return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
}
override fun onCreateSession(): Session {
return CarHomeSession()
}
}
class CarHomeSession : Session() {
override fun onCreateScreen(intent: Intent): Screen {
return CarHomeScreen(carContext)
}
}
class CarHomeScreen(carContext: CarContext) : Screen(carContext)
override fun onGetTemplate(): Template {
val myPane = Pane.Builder().addRow(Row.Builder().setTitle("Row 1").build()).build()
val myHeader = Header.Builder().setTitle("Header").build()
val myPaneTemplate = PaneTemplate.Builder(myPane)
.setHeader(myHeader)
.build()
val myActionStrip = ActionStrip.Builder()
.addAction(Action.APP_ICON)
.addAction(Action.PAN)
.addAction(Action.BACK)
.build()
val myMapController = MapController.Builder()
.setMapActionStrip(myActionStrip)
.build()
val myMwcTemplate = MapWithContentTemplate.Builder()
.setContentTemplate(myPaneTemplate)
.setMapController(myMapController)
.build()
return myMwcTemplate
}
}
这是清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="androidx.car.app.MAP_TEMPLATES"/>
<uses-permission android:name="androidx.car.app.ACCESS_SURFACE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:label="AutoGlucose"
android:name="${applicationName}"
android:icon="@mipmap/logo">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="@xml/automotive_app_desc"/>
<meta-data android:name="androidx.car.app.minCarApiLevel"
android:value="1" />
<service
android:name=".CarHomeService"
android:stopWithTask="false"
android:exported="true">
<intent-filter>
<action android:name="androidx.car.app.CarAppService" />
<category android:name="androidx.car.app.category.POI"/>
</intent-filter>
</service>
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
代表您绘制地图的唯一模板是
PlaceListMapTemplate
,它只能由POI应用程序使用。
MapWithContentTemplate
可供 POI 应用程序(以及天气和导航应用程序)使用,需要您自己绘制地图。有关如何执行此操作的更多详细信息,请参阅 https://developer.android.com/training/cars/apps#draw-maps。