You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求教:如何开发适用于Android Auto的导航应用?

Android Auto导航模板实现步骤(新手向)

1. 项目基础配置

先完成两个核心配置:

  • 打开模块级build.gradle(Module: app),在dependencies块添加Android Auto依赖:
    implementation 'androidx.car.app:app:1.4.0'
    implementation 'androidx.car.app:navigation:1.4.0'
    
  • 打开AndroidManifest.xml,添加权限和Auto服务声明:
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
    <application ...>
      <service
          android:name=".MyCarAppService"
          android:exported="true">
          <intent-filter>
              <action android:name="androidx.car.app.CarAppService" />
              <category android:name="androidx.car.app.category.NAVIGATION" />
          </intent-filter>
          <meta-data
              android:name="androidx.car.app.minCarApiLevel"
              android:value="1" />
      </service>
    </application>
    

2. 创建CarAppService子类

新建类继承CarAppService,作为Auto应用的入口:

class MyCarAppService : CarAppService() {
    override fun onCreateSession(): Session {
        return MyNavigationSession()
    }
}

3. 实现导航Session类

Session管理界面生命周期,新建子类并返回导航界面:

class MyNavigationSession : Session() {
    override fun onCreateScreen(intent: Intent): Screen {
        return NavigationScreen(carContext)
    }
}

4. 构建导航模板界面

创建NavigationScreen类,组装导航模板的核心内容:

class NavigationScreen(carContext: CarContext) : Screen(carContext) {
    override fun onGetTemplate(): Template {
        // 示例转向指令
        val maneuver = Maneuver.Builder()
            .setType(Maneuver.TYPE_TURN_RIGHT)
            .setTurnAngle(Maneuver.TURN_ANGLE_SHARP)
            .build()

        // 示例路线预估信息
        val routeEstimate = RouteTravelEstimate.Builder()
            .setRemainingDistance(Distance.create(1500, Distance.UNIT_METERS))
            .setRemainingTimeSeconds(120)
            .build()

        // 组装导航模板
        return NavigationTemplate.Builder()
            .setTitle("我的导航")
            .setCurrentManeuver(maneuver)
            .setCurrentRouteTravelEstimate(routeEstimate)
            .build()
    }
}

5. 测试说明

  • 用Android Studio创建Automotive模拟器(Device Manager里选Automotive类型设备)
  • 启动模拟器后,在应用列表找到你的应用即可查看导航模板效果
  • 实车测试需在手机开发者选项开启「Android Auto调试」,再连接车机

内容的提问来源于stack exchange,提问作者downmath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:54:25