Android 车载应用存储方式指南

Android Automotive OS 和 Android Auto 应用开发中可用的存储方式需要特别考虑车辆环境的限制和安全性要求。以下是主要的存储选项及其使用场景:

1. 应用专属存储 (App-Specific Storage)

最适合车载环境的基础存储方案

// 获取应用内部存储目录
val internalFilesDir = context.filesDir
val internalCacheDir = context.cacheDir

// 写入文件
File(internalFilesDir, "config.json").writeText("{...}")

// 外部存储(需要权限)
val externalFilesDir = context.getExternalFilesDir(null)

特点

  • 无需权限即可访问

  • 应用卸载时自动清除

  • 适合存储配置、缓存等私有数据

2. 共享存储 (MediaStore)

适用于多媒体内容的存储

// 保存图片到共享存储
val values = ContentValues().apply {
    put(MediaStore.Images.Media.DISPLAY_NAME, "my_car_photo.jpg")
    put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
    put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CarApp")
}

val uri = contentResolver.insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
    values
)

// 写入数据
contentResolver.openOutputStream(uri)?.use { 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it)
}

特点

  • 需要 READ_EXTERNAL_STORAGE/WRITE_EXTERNAL_STORAGE 权限

  • 适合用户可见的多媒体文件

  • 车载系统可能限制某些媒体类型的访问

3. 偏好设置 (SharedPreferences)

轻量级键值对存储

// 获取SharedPreferences
val prefs = context.getSharedPreferences(
    "CarAppPrefs",
    Context.MODE_PRIVATE
)

// 写入数据
prefs.edit {
    putString("last_connected_device", "BMW-X5")
    putInt("volume_level", 80)
    apply()
}

// 读取数据
val volume = prefs.getInt("volume_level", 50)

特点

  • 适合小量简单数据

  • 自动持久化

  • 线程安全

4. Room 数据库

结构化数据存储方案

// 定义Entity
@Entity
data class CarSettings(
    @PrimaryKey val id: Int,
    val seatPosition: String,
    val mirrorAdjustment: String
)

// 定义DAO
@Dao
interface CarSettingsDao {
    @Insert
    fun insert(settings: CarSettings)
    
    @Query("SELECT * FROM CarSettings WHERE id = :id")
    fun getSettings(id: Int): CarSettings?
}

// 使用数据库
val db = Room.databaseBuilder(
    context,
    AppDatabase::class.java, "car-db"
).build()

特点

  • 适合复杂结构化数据

  • 提供类型安全查询

  • 支持LiveData观察变化

5. 车载系统特定存储

针对Automotive OS的扩展存储

// 获取汽车属性存储(需要Car权限)
CarPropertyManager carPropertyManager = 
    (CarPropertyManager) getSystemService(CAR_PROPERTY_SERVICE);

// 读取车辆属性
Float cabinTemp = carPropertyManager.getPropertyFloat(
    VehiclePropertyIds.CABIN_TEMPERATURE, 
    VehicleAreaSeat.SEAT_ROW_1_LEFT
);

特点

  • 需要 CAR_INFO 权限

  • 访问车辆特定数据

  • 必须通过 Car API访问

存储选择建议

存储类型 适合场景 数据保留策略 是否需要权限
应用专属存储 应用私有数据、缓存 应用卸载时清除 不需要
共享存储 用户多媒体文件 持久化保留 需要
SharedPreferences 用户偏好设置 应用卸载时清除 不需要
Room数据库 结构化应用数据 应用卸载时清除 不需要
车载系统存储 车辆状态信息 系统决定 需要CAR权限

特殊注意事项

  1. 存储限制

    • 车载系统通常有更严格的存储配额

    • 避免在车辆环境下存储大文件

  2. 数据加密

    // 使用EncryptedSharedPreferences
    val masterKey = MasterKey.Builder(context)
        .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
        .build()
    
    val securePrefs = EncryptedSharedPreferences.create(
        context,
        "secure_car_prefs",
        masterKey,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    )
  3. 备份策略

    • 车载应用数据默认不参与自动备份

    • 如需备份需实现自定义方案

  4. 多用户支持

    • 车辆可能有多个驾驶员配置

    • 需要为每个用户单独存储设置

      // 获取当前用户上下文
      val userContext = context.createContextAsUser(
          UserHandle.getUserHandleForUid(Process.myUid()), 
          0
      )

在车载环境中,应优先考虑使用应用专属存储和SharedPreferences,仅在必要时访问共享存储或车辆系统存储,并始终遵循最小权限原则。

你可能感兴趣的:(智能座舱,android)