删除数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
删除数据是 Health Connect 中 CRUD 操作的关键环节。本指南将为您介绍如何通过以下两种方式删除记录。
使用记录 ID 进行删除
您可以使用唯一标识符(例如记录 ID 和应用的客户端记录 ID)列表来删除记录。具体方法是:使用 deleteRecords
,并为其提供两个 Strings
列表,一个用于记录 ID,另一个用于客户端 ID。如果您只能提供其中一种 ID,可以在另一个列表中设置 emptyList()
。
以下代码示例展示了如何使用相关 ID 删除步数数据:
suspend fun deleteStepsByUniqueIdentifier(
healthConnectClient: HealthConnectClient,
idList: List<String>
) {
try {
healthConnectClient.deleteRecords(
StepsRecord::class,
idList = idList,
clientRecordIdsList = emptyList()
)
} catch (e: Exception) {
// Run error handling here
}
}
使用时间范围进行删除
您还可以将时间范围用作过滤条件来删除数据。具体方法是:使用 deleteRecords
,并为其提供带有开始时间戳值和结束时间戳值的 TimeRangeFilter
对象。
以下代码示例展示了如何删除特定时间的步数数据:
suspend fun deleteStepsByTimeRange(
healthConnectClient: HealthConnectClient,
startTime: Instant,
endTime: Instant
) {
try {
healthConnectClient.deleteRecords(
StepsRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
} catch (e: Exception) {
// Run error handling here
}
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Delete data\n\nDeleting data is a key part of the CRUD operations in Health Connect. This guide\nshows you how you can delete records in two ways.\n| **Tip:** For further guidance on deleting data, take a look at the [Android Developer video for reading and writing data](https://www.youtube.com/watch?v=NAx7Gv_Hk7E&t=299) in Health Connect.\n\nDelete using Record IDs\n-----------------------\n\nYou can delete records using a list of unique identifiers such as the Record ID\nand your app's Client Record ID. Use [`deleteRecords`](/reference/kotlin/androidx/health/connect/client/HealthConnectClient#deleteRecords(kotlin.reflect.KClass,kotlin.collections.List,kotlin.collections.List)), and\nsupply it with two lists of `Strings`, one for the Record IDs and one for the\nClient IDs. If you only have one of the IDs available, you can set `emptyList()`\non the other list.\n\nThe following code example shows how to delete Steps data using its IDs: \n\n suspend fun deleteStepsByUniqueIdentifier(\n healthConnectClient: HealthConnectClient,\n idList: List\u003cString\u003e\n ) {\n try {\n healthConnectClient.deleteRecords(\n StepsRecord::class,\n idList = idList,\n clientRecordIdsList = emptyList()\n )\n } catch (e: Exception) {\n // Run error handling here\n }\n }\n\nDelete using a time range\n-------------------------\n\nYou can also delete data using a time range as your filter.\nUse [`deleteRecords`](/reference/kotlin/androidx/health/connect/client/HealthConnectClient#deleteRecords(kotlin.reflect.KClass,androidx.health.connect.client.time.TimeRangeFilter)), and supply it with a\n[`TimeRangeFilter`](/reference/kotlin/androidx/health/connect/client/time/TimeRangeFilter) object that takes\na start and end timestamp values.\n\nThe following code example shows how to delete data of Steps data on a\nspecific time: \n\n suspend fun deleteStepsByTimeRange(\n healthConnectClient: HealthConnectClient,\n startTime: Instant,\n endTime: Instant\n ) {\n try {\n healthConnectClient.deleteRecords(\n StepsRecord::class,\n timeRangeFilter = TimeRangeFilter.between(startTime, endTime)\n )\n } catch (e: Exception) {\n // Run error handling here\n }\n }"]]