功能的适用范围
如需确定用户的设备是否支持 Health Connect 上的锻炼计划,请检查客户端上 FEATURE_PERSONAL_HEALTH_RECORD
的可用性:
if (healthConnectClient
.features
.getFeatureStatus(
HealthConnectFeatures.FEATURE_PERSONAL_HEALTH_RECORD
) == HealthConnectFeatures.FEATURE_STATUS_AVAILABLE) {
// Feature is available
} else {
// Feature isn't available
}
如需了解详情,请参阅查看功能可用性。
所需权限
对个人健康记录的访问权限受以下权限保护:
android.permission.health.WRITE_MEDICAL_DATA
android.permission.health.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES
android.permission.health.READ_MEDICAL_DATA_CONDITIONS
android.permission.health.READ_MEDICAL_DATA_LABORATORY_RESULTS
android.permission.health.READ_MEDICAL_DATA_MEDICATIONS
android.permission.health.READ_MEDICAL_DATA_PERSONAL_DETAILS
android.permission.health.READ_MEDICAL_DATA_PRACTITIONER_DETAILS
android.permission.health.READ_MEDICAL_DATA_PREGNANCY
android.permission.health.READ_MEDICAL_DATA_PROCEDURES
android.permission.health.READ_MEDICAL_DATA_SOCIAL_HISTORY
android.permission.health.READ_MEDICAL_DATA_VACCINES
android.permission.health.READ_MEDICAL_DATA_VISITS
android.permission.health.READ_MEDICAL_DATA_VITAL_SIGNS
请在 Play 管理中心内为您的应用声明以下权限,并在应用的清单中声明这些权限:
<application>
<uses-permission
android:name="android.permission.health.WRITE_MEDICAL_DATA" />
<uses-permission
android:name="android.permission.health.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES" />
...
</application>
您有责任声明您打算在设备和应用中使用的所有适当权限。您还应在使用前检查用户是否已授予每项权限。
用法示例
本部分介绍了即将发布的支持 PHR 的 Jetpack SDK 版本的基本操作的代码示例,这些示例可能会发生变化。
创建 MedicalDataSource 记录
// Create a `MedicalDataSource`
// Note that `displayName` must be unique across `MedicalDataSource`s
// Each `MedicalDataSource` is assigned an `id` by the system on creation
val medicalDataSource: MedicalDataSource =
healthConnectClient.createMedicalDataSource(
CreateMedicalDataSourceRequest(
fhirBaseUri = Uri.parse("https://fhir.com/oauth/api/FHIR/R4/"),
displayName = "Test Data Source",
fhirVersion = FhirVersion(4, 0, 1)
)
)
删除 MedicalDataSource 记录
在上面的示例中,系统会在创建时返回 id
。如需删除,请引用同一 id
:
// Delete the `MedicalDataSource` that has the specified `id`
healthConnectClient.deleteMedicalDataSourceWithData(medicalDataSource.id)
按软件包名称获取 MedicalDataSource 记录
使用 GetMedicalDataSourcesRequest
按软件包名称(应用)进行请求:
// Retrieve all `MedicalDataSource`s created by any of the specified package names
// Package names may be found in other `MedicalDataSource`s or from arbitrary input
val medicalDataSources: List<MedicalDataSource> =
healthConnectClient.getMedicalDataSources(
GetMedicalDataSourcesRequest(listOf(medicalDataSource.packageName, anotherPackageName))
)
按 ID 获取 MedicalDataSource 记录
或者,如果您知道 id
,也可以按 id
进行请求:
// Retrieve all `MedicalDataSource` with `id` matching any of the given ids
val medicalDataSources: List<MedicalDataSource> =
healthConnectClient.getMedicalDataSources(listOf(medicalDataSource.id, anotherId))
插入或更新 MedicalResource 记录
使用 UpsertMedicalResourceRequest
为 MedicalDataSource
插入新的 MedicalResource
记录或更新现有 MedicalResource
记录:
// Insert `MedicalResource`s into the `MedicalDataSource`
val medicalResources: List<MedicalResource> =
healthConnectClient.upsertMedicalResources(
listOf(
UpsertMedicalResourceRequest(
medicalDataSource.id,
medicalDataSource.fhirVersion,
medicationJsonToInsert // a valid FHIR json string
)
)
)
// Update `MedicalResource`s in the `MedicalDataSource`
val updatedMedicalResources: List<MedicalResource> =
healthConnectClient.upsertMedicalResources(
listOf(
UpsertMedicalResourceRequest(
medicalDataSource.id,
medicalDataSource.fhirVersion,
// a valid FHIR json string
// if this resource has the same type and ID as in `medicationJsonToInsert`,
// this `upsertMedicalResources()` call will update the previously inserted
// `MedicalResource`
updatedMedicationJsonToInsert
)
)
)
获取 MedicalResource 记录
通过指定 medicalResourceType
过滤 get 请求。请务必使用分页请求,并注意速率限制。
// Read `MedicalResource`s back from the `MedicalDataSource`
// Read 100 resources / page. See `pageSize` doc for defaults and limits.
val pageSize = 100
// Prepare the initial read request.
// All `MedicalResource`s in the given `MedicalDataSource`s and of given `medicalResourceType`
// will be retrieved.
val initialRequest: ReadMedicalResourcesRequest =
ReadMedicalResourcesInitialRequest(
MEDICAL_RESOURCE_TYPE_LABORATORY_RESULTS,
setOf(medicalDataSource.id),
pageSize = pageSize,
)
// Continue reading pages until all `MedicalResource`s are read
var pageToken: String? = null
do {
// Prepare paged request if needed
val request: ReadMedicalResourcesRequest =
if (pageToken == null) initialRequest
else ReadMedicalResourcesPageRequest(pageToken, pageSize = pageSize)
// Read `MedicalResource`s
val response: ReadMedicalResourcesResponse =
healthConnectClient.readMedicalResources(request)
// Process `MedicalResource`s
val resources: List<MedicalResource> = response.medicalResources
// Advance to next page
pageToken = response.nextPageToken
} while (pageToken != null)
按 ID 获取 MedicalResource 记录
// Retrieve `fhirResourceType` type `MedicalResource`s with the specified `id`s from the
// provided `MedicalDataSource`
val retrievedMedicalResources: List<MedicalResource> =
healthConnectClient.readMedicalResources(
medicalResources.map { medicalResource: MedicalResource ->
MedicalResourceId(
dataSourceId = medicalDataSource.id,
fhirResourceType = medicalResource.id.fhirResourceType,
fhirResourceId = medicalResource.id.fhirResourceId
)
}
)
按 ID 删除 MedicalResource 记录
MedicalResource
记录可能会按 ID 删除:
// Delete `MedicalResource`s matching the specified `dataSourceId`, `type` and `fhirResourceId`
healthConnectClient.deleteMedicalResources(
medicalResources.map { medicalResource: MedicalResource ->
MedicalResourceId(
dataSourceId = medicalDataSource.id,
fhirResourceType = medicalResource.id.fhirResourceType,
fhirResourceId = medicalResource.id.fhirResourceId
)
}
)
或者,您也可以使用 medicalResourceType
删除它们:
// Delete all `MedicalResource`s that are in any pair of provided `dataSourceIds` and
// `medicalResourceTypes`
healthConnectClient.deleteMedicalResources(
DeleteMedicalResourcesRequest(
dataSourceIds = setOf(medicalDataSource.id),
medicalResourceTypes = setOf(MEDICAL_RESOURCE_TYPE_MEDICATIONS)
)
)