1e41f4b71Sopenharmony_ci# @ohos.hiviewdfx.hiAppEvent (应用事件打点) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供了应用事件打点能力,包括应用事件落盘、应用事件订阅、应用事件清理、打点功能配置等功能。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { hiAppEvent } from '@kit.PerformanceAnalysisKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## hiAppEvent.addProcessor<sup>11+</sup> 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciaddProcessor(processor: Processor): number 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci开发者可添加数据处理者,该数据处理者用于提供事件上云功能,数据处理者的实现可预置在设备中,开发者可根据数据处理者的约束设置属性。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciProcessor的配置信息需要由数据处理者提供,目前设备内暂未预置可供交互的数据处理者,因此当前事件上云功能不可用。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**参数:** 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 31e41f4b71Sopenharmony_ci| --------- | ---------- | ---- | ------------- | 32e41f4b71Sopenharmony_ci| processor | [Processor](#processor11) | 是 | 上报事件的数据处理者。| 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**返回值:** 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci| 类型 | 说明 | 37e41f4b71Sopenharmony_ci| ------ | ---------------------- | 38e41f4b71Sopenharmony_ci| number | 所添加上报事件数据处理者的ID。 添加失败返回-1,添加成功返回大于0的值。 | 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**错误码:** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 43e41f4b71Sopenharmony_ci| ------- | ----------------- | 44e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci**示例:** 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci```ts 49e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_citry { 52e41f4b71Sopenharmony_ci let processor: hiAppEvent.Processor = { 53e41f4b71Sopenharmony_ci name: 'analytics_demo' 54e41f4b71Sopenharmony_ci }; 55e41f4b71Sopenharmony_ci let id: number = hiAppEvent.addProcessor(processor); 56e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `addProcessor event was successful, id=${id}`); 57e41f4b71Sopenharmony_ci} catch (error) { 58e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to addProcessor event, code=${error.code}`); 59e41f4b71Sopenharmony_ci} 60e41f4b71Sopenharmony_ci``` 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci## Processor<sup>11+</sup> 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci可以上报事件的数据处理者对象。 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 69e41f4b71Sopenharmony_ci| ------------------- | ----------------------- | ---- | ---------------------------------------------------------------------------------------------------------- | 70e41f4b71Sopenharmony_ci| name | string | 是 | 数据处理者的名称。名称只能包含大小写字母、数字、下划线和 $,不能以数字开头,长度非空且不超过256个字符。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 71e41f4b71Sopenharmony_ci| debugMode | boolean | 否 | 是否开启debug模式,默认值为false。配置值为true表示开启debug模式,false表示不开启debug模式。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 72e41f4b71Sopenharmony_ci| routeInfo | string | 否 | 服务器位置信息,默认为空字符串。传入字符串长度不能超过8KB,超过时会被置为默认值。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 73e41f4b71Sopenharmony_ci| appId | string | 否 | 应用id,默认为空字符串。传入字符串长度不能超过8KB,超过时会被置为默认值。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 74e41f4b71Sopenharmony_ci| onStartReport | boolean | 否 | 数据处理者在启动时是否上报事件,默认值为false。配置值为true表示上报事件,false表示不上报事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 75e41f4b71Sopenharmony_ci| onBackgroundReport | boolean | 否 | 当应用程序进入后台时是否上报事件,默认值为false。配置值为true表示上报事件,false表示不上报事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 76e41f4b71Sopenharmony_ci| periodReport | number | 否 | 事件定时上报时间周期,单位为秒。传入数值必须大于或等于0,小于0时会被置为默认值0,不进行定时上报。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 77e41f4b71Sopenharmony_ci| batchReport | number | 否 | 事件上报阈值,当事件条数达到阈值时上报事件。传入数值必须大于0且小于1000,不在数值范围内会被置为默认值0,不进行上报。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 78e41f4b71Sopenharmony_ci| userIds | string[] | 否 | 数据处理者可以上报的用户ID的name数组。name对应[setUserId](#hiappeventsetuserid11)接口的name参数。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 79e41f4b71Sopenharmony_ci| userProperties | string[] | 否 | 数据处理者可以上报的用户属性的name数组。name对应[setUserProperty](#hiappeventsetuserproperty11)接口的name参数。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 80e41f4b71Sopenharmony_ci| eventConfigs | [AppEventReportConfig](#appeventreportconfig11)[] | 否 | 数据处理者可以上报的事件数组。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 81e41f4b71Sopenharmony_ci| configId<sup>12+</sup> | number | 否 | 数据处理者配置id。传入数值必须大于或等于0,小于0时会被置为默认值0。传入的值大于0时,与数据处理者的名称name共同唯一标识数据处理者。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 82e41f4b71Sopenharmony_ci| customConfigs<sup>12+</sup> | Record\<string, string> | 否 | 自定义扩展参数。传入参数名和参数值不符合规格会默认不配置扩展参数,其规格定义如下:<br>- 参数名为string类型,首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符,长度非空且不超过32个字符。<br>- 参数值为string类型,参数值长度需在1024个字符以内。<br>- 参数个数需在32个以内。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci## AppEventReportConfig<sup>11+</sup> 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci数据处理者可以上报事件的描述配置。 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 93e41f4b71Sopenharmony_ci| ----------- | ------- | ---- | ------------------------------------------------------------ | 94e41f4b71Sopenharmony_ci| domain | string | 否 | 事件领域。事件领域名称支持数字、字母、下划线字符,需要以字母开头且不能以下划线结尾,长度非空且不超过32个字符。 | 95e41f4b71Sopenharmony_ci| name | string | 否 | 事件名称。首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符,长度非空且不超过48个字符。 | 96e41f4b71Sopenharmony_ci| isRealTime | boolean | 否 | 是否实时上报事件。配置值为true表示实时上报事件,false表示不实时上报事件。 | 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci## hiAppEvent.removeProcessor<sup>11+</sup> 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciremoveProcessor(id: number): void 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci删除上报事件的数据处理者。 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci**参数:** 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 111e41f4b71Sopenharmony_ci| ------| ------- | ---- | --------------------------- | 112e41f4b71Sopenharmony_ci| id | number | 是 | 上报事件数据处理者ID。值大于0。| 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**错误码:** 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 117e41f4b71Sopenharmony_ci| ------- | ----------------- | 118e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**示例:** 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci```ts 123e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_citry { 126e41f4b71Sopenharmony_ci let processor: hiAppEvent.Processor = { 127e41f4b71Sopenharmony_ci name: 'analytics_demo' 128e41f4b71Sopenharmony_ci }; 129e41f4b71Sopenharmony_ci let id: number = hiAppEvent.addProcessor(processor); 130e41f4b71Sopenharmony_ci hiAppEvent.removeProcessor(id); 131e41f4b71Sopenharmony_ci} catch (error) { 132e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to removeProcessor event, code=${error.code}`); 133e41f4b71Sopenharmony_ci} 134e41f4b71Sopenharmony_ci``` 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci## hiAppEvent.write 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ciwrite(info: AppEventInfo, callback: AsyncCallback<void>): void 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci应用事件打点方法,将事件写入到当天的事件文件中,可接收AppEventInfo类型的事件对象,使用callback方式作为异步回调。 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci**参数:** 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 149e41f4b71Sopenharmony_ci| -------- | ------------------------------ | ---- | -------------- | 150e41f4b71Sopenharmony_ci| info | [AppEventInfo](#appeventinfo) | 是 | 应用事件对象。 | 151e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 打点回调函数。 | 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**错误码:** 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 158e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | 159e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 160e41f4b71Sopenharmony_ci| 11100001 | Function disabled. | 161e41f4b71Sopenharmony_ci| 11101001 | Invalid event domain. | 162e41f4b71Sopenharmony_ci| 11101002 | Invalid event name. | 163e41f4b71Sopenharmony_ci| 11101003 | Invalid number of event parameters. | 164e41f4b71Sopenharmony_ci| 11101004 | Invalid string length of the event parameter. | 165e41f4b71Sopenharmony_ci| 11101005 | Invalid event parameter name. | 166e41f4b71Sopenharmony_ci| 11101006 | Invalid array length of the event parameter. | 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci**示例:** 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci```ts 171e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 172e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_cilet eventParams: Record<string, number | string> = { 175e41f4b71Sopenharmony_ci "int_data": 100, 176e41f4b71Sopenharmony_ci "str_data": "strValue", 177e41f4b71Sopenharmony_ci}; 178e41f4b71Sopenharmony_cihiAppEvent.write({ 179e41f4b71Sopenharmony_ci domain: "test_domain", 180e41f4b71Sopenharmony_ci name: "test_event", 181e41f4b71Sopenharmony_ci eventType: hiAppEvent.EventType.FAULT, 182e41f4b71Sopenharmony_ci params: eventParams, 183e41f4b71Sopenharmony_ci}, (err: BusinessError) => { 184e41f4b71Sopenharmony_ci if (err) { 185e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `code: ${err.code}, message: ${err.message}`); 186e41f4b71Sopenharmony_ci return; 187e41f4b71Sopenharmony_ci } 188e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `success to write event`); 189e41f4b71Sopenharmony_ci}); 190e41f4b71Sopenharmony_ci``` 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci## hiAppEvent.write 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciwrite(info: AppEventInfo): Promise<void> 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci应用事件打点方法,将事件写入到当天的事件文件中,可接收AppEventInfo类型的事件对象,使用Promise方式作为异步回调。 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**参数:** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 205e41f4b71Sopenharmony_ci| ------ | ------------------------------ | ---- | -------------- | 206e41f4b71Sopenharmony_ci| info | [AppEventInfo](#appeventinfo) | 是 | 应用事件对象。 | 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci**返回值:** 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci| 类型 | 说明 | 211e41f4b71Sopenharmony_ci| ------------------- | ------------- | 212e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。 | 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci**错误码:** 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 219e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | 220e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 221e41f4b71Sopenharmony_ci| 11100001 | Function disabled. | 222e41f4b71Sopenharmony_ci| 11101001 | Invalid event domain. | 223e41f4b71Sopenharmony_ci| 11101002 | Invalid event name. | 224e41f4b71Sopenharmony_ci| 11101003 | Invalid number of event parameters. | 225e41f4b71Sopenharmony_ci| 11101004 | Invalid string length of the event parameter. | 226e41f4b71Sopenharmony_ci| 11101005 | Invalid event parameter name. | 227e41f4b71Sopenharmony_ci| 11101006 | Invalid array length of the event parameter. | 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**示例:** 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci```ts 232e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 233e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_cilet eventParams: Record<string, number | string> = { 236e41f4b71Sopenharmony_ci "int_data": 100, 237e41f4b71Sopenharmony_ci "str_data": "strValue", 238e41f4b71Sopenharmony_ci}; 239e41f4b71Sopenharmony_cihiAppEvent.write({ 240e41f4b71Sopenharmony_ci domain: "test_domain", 241e41f4b71Sopenharmony_ci name: "test_event", 242e41f4b71Sopenharmony_ci eventType: hiAppEvent.EventType.FAULT, 243e41f4b71Sopenharmony_ci params: eventParams, 244e41f4b71Sopenharmony_ci}).then(() => { 245e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `success to write event`); 246e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 247e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `code: ${err.code}, message: ${err.message}`); 248e41f4b71Sopenharmony_ci}); 249e41f4b71Sopenharmony_ci``` 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci## AppEventInfo 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci提供了应用事件信息的参数选项。 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 260e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 261e41f4b71Sopenharmony_ci| domain | string | 是 | 事件领域。事件领域名称支持数字、字母、下划线字符,需要以字母开头且不能以下划线结尾,长度非空且不超过32个字符。 | 262e41f4b71Sopenharmony_ci| name | string | 是 | 事件名称。首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符,长度非空且不超过48个字符。 | 263e41f4b71Sopenharmony_ci| eventType | [EventType](#eventtype) | 是 | 事件类型。 | 264e41f4b71Sopenharmony_ci| params | object | 是 | 事件参数对象,每个事件参数包括参数名和参数值。**系统事件中params包含的字段已由各系统事件定义,具体字段含义在各类系统事件指南的介绍中,例如[崩溃事件介绍](../../dfx/hiappevent-watcher-crash-events.md)。** 针对应用事件,打点写入的参数由开发者定义,其规格如下:<br>- 参数名为string类型,首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符,长度非空且不超过32个字符。<br>- 参数值支持string、number、boolean、数组类型,string类型参数长度需在8*1024个字符以内,超出会做丢弃处理;number类型参数取值需在Number.MIN_SAFE_INTEGER~Number.MAX_SAFE_INTEGER范围内,超出可能会产生不确定值;数组类型参数中的元素类型只能全为string、number、boolean中的一种,且元素个数需在100以内,超出会做丢弃处理。<br>- 参数个数需在32个以内,超出的参数会做丢弃处理。 | 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci## hiAppEvent.setEventParam<sup>12+</sup> 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_cisetEventParam(params: Record<string, ParamType>, domain: string, name?: string): Promise<void> 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci事件自定义参数设置方法,使用Promise方式作为异步回调。在同一生命周期中,可以通过事件领域和事件名称关联系统事件和应用事件,系统事件仅支持崩溃和卡死事件。 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci**参数:** 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 279e41f4b71Sopenharmony_ci| ------ | ------------------------------ | ---- | -------------- | 280e41f4b71Sopenharmony_ci| params | Record<string, [ParamType](#paramtype12)> | 是 | 事件自定义参数对象。参数名和参数值规格定义如下:<br>- 参数名为string类型,首字符必须为字母字符或$字符,中间字符必须为数字字符、字母字符或下划线字符,结尾字符必须为数字字符或字母字符,长度非空且不超过32个字符。<br>- 参数值为[ParamType](#paramtype12)类型,参数值长度需在1024个字符以内。<br>- 参数个数需在64个以内。 | 281e41f4b71Sopenharmony_ci| domain | string | 是 | 事件领域。事件领域可支持关联应用事件和系统事件(hiAppEvent.domain.OS)。 | 282e41f4b71Sopenharmony_ci| name | string | 否 | 事件名称。默认为空字符串,空字符串表示关联事件领域下的所有事件名称。事件名称可支持关联应用事件和系统事件,其中系统事件仅支持关联崩溃事件(hiAppEvent.event.APP_CRASH)和卡死事件(hiAppEvent.event.APP_FREEZE)。 | 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci**返回值:** 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci| 类型 | 说明 | 287e41f4b71Sopenharmony_ci| ------------------- | ------------- | 288e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。 | 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**错误码:** 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 295e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | 296e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 297e41f4b71Sopenharmony_ci| 11101007 | The number of parameter keys exceeds the limit. | 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**示例:** 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci```ts 302e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 303e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_cilet params: Record<string, hiAppEvent.ParamType> = { 306e41f4b71Sopenharmony_ci "int_data": 100, 307e41f4b71Sopenharmony_ci "str_data": "strValue", 308e41f4b71Sopenharmony_ci}; 309e41f4b71Sopenharmony_ci// 给应用事件追加自定义参数 310e41f4b71Sopenharmony_cihiAppEvent.setEventParam(params, "test_domain", "test_event").then(() => { 311e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `success to set svent param`); 312e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 313e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `code: ${err.code}, message: ${err.message}`); 314e41f4b71Sopenharmony_ci}); 315e41f4b71Sopenharmony_ci``` 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci## ParamType<sup>12+</sup> 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_citype ParamType = number | string | boolean | Array<string> 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci事件自定义参数值的类型。 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci| 类型 | 说明 | 328e41f4b71Sopenharmony_ci|--------------------------|-------------------| 329e41f4b71Sopenharmony_ci| number | 表示值类型为数字。 | 330e41f4b71Sopenharmony_ci| string | 表示值类型为字符串。 | 331e41f4b71Sopenharmony_ci| boolean | 表示值类型为布尔值。 | 332e41f4b71Sopenharmony_ci| Array<string> | 表示值类型为字符串类型的数组。 | 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci## hiAppEvent.configure 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ciconfigure(config: ConfigOption): void 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci应用事件打点配置方法,可用于配置打点开关、目录存储配额大小等功能。 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**参数:** 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 347e41f4b71Sopenharmony_ci| ------ | ----------------------------- | ---- | ------------------------ | 348e41f4b71Sopenharmony_ci| config | [ConfigOption](#configoption) | 是 | 应用事件打点配置项对象。 | 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci**错误码:** 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 355e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 356e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 357e41f4b71Sopenharmony_ci| 11103001 | Invalid max storage quota value. | 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci**示例:** 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci```ts 362e41f4b71Sopenharmony_ci// 配置打点开关为关闭状态 363e41f4b71Sopenharmony_cilet config1: hiAppEvent.ConfigOption = { 364e41f4b71Sopenharmony_ci disable: true, 365e41f4b71Sopenharmony_ci}; 366e41f4b71Sopenharmony_cihiAppEvent.configure(config1); 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci// 配置文件目录存储配额为100M 369e41f4b71Sopenharmony_cilet config2: hiAppEvent.ConfigOption = { 370e41f4b71Sopenharmony_ci maxStorage: '100M', 371e41f4b71Sopenharmony_ci}; 372e41f4b71Sopenharmony_cihiAppEvent.configure(config2); 373e41f4b71Sopenharmony_ci``` 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci## ConfigOption 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci提供了对应用事件打点功能的配置选项。 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 384e41f4b71Sopenharmony_ci| ---------- | ------- | ---- | ------------------------------------------------------------ | 385e41f4b71Sopenharmony_ci| disable | boolean | 否 | 打点功能开关,默认值为false。true:关闭打点功能,false:不关闭打点功能。 | 386e41f4b71Sopenharmony_ci| maxStorage | string | 否 | 打点数据存放目录的配额大小,默认值为“10M”。<br>在目录大小超出配额后,下次打点会触发对目录的清理操作:按从旧到新的顺序逐个删除打点数据文件,直到目录大小不超出配额时结束。<br>配额值字符串规格如下:<br>- 配额值字符串只由数字字符和大小单位字符(单位字符支持[b\|k\|kb\|m\|mb\|g\|gb\|t\|tb],不区分大小写)构成。<br>- 配额值字符串必须以数字开头,后面可以选择不传单位字符(默认使用byte作为单位),或者以单位字符结尾。 | 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci## hiAppEvent.setUserId<sup>11+</sup> 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_cisetUserId(name: string, value: string): void 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci设置用户ID。 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**参数:** 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 401e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | ------------- | 402e41f4b71Sopenharmony_ci| name | string | 是 | 用户ID的key。只能包含大小写字母、数字、下划线和 $,不能以数字开头,长度非空且不超过256个字符。 | 403e41f4b71Sopenharmony_ci| value | string | 是 | 用户ID的值。长度不超过256,当值为null或空字符串时,则清除用户ID。 | 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci**错误码:** 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 408e41f4b71Sopenharmony_ci| ------- | ----------------- | 409e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 410e41f4b71Sopenharmony_ci 411e41f4b71Sopenharmony_ci**示例:** 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci```ts 414e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_citry { 417e41f4b71Sopenharmony_ci hiAppEvent.setUserId('key', 'value'); 418e41f4b71Sopenharmony_ci} catch (error) { 419e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to setUserId event, code=${error.code}`); 420e41f4b71Sopenharmony_ci} 421e41f4b71Sopenharmony_ci``` 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci## hiAppEvent.getUserId<sup>11+</sup> 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_cigetUserId(name: string): string 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci获取之前通过setUserId接口设置的value值。 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci**参数:** 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 436e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | ---------- | 437e41f4b71Sopenharmony_ci| name | string | 是 | 用户ID的key。只能包含大小写字母、数字、下划线和 $,不能以数字开头,长度不超过256。| 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci**返回值:** 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci| 类型 | 说明 | 442e41f4b71Sopenharmony_ci| ------ | ------------------------------- | 443e41f4b71Sopenharmony_ci| string | 用户ID的值。没有查到返回空字符串。 | 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ci**错误码:** 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 448e41f4b71Sopenharmony_ci| ------- | ----------------- | 449e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ci**示例:** 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci```ts 454e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_cihiAppEvent.setUserId('key', 'value'); 457e41f4b71Sopenharmony_citry { 458e41f4b71Sopenharmony_ci let value: string = hiAppEvent.getUserId('key'); 459e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `getUserId event was successful, userId=${value}`); 460e41f4b71Sopenharmony_ci} catch (error) { 461e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to getUserId event, code=${error.code}`); 462e41f4b71Sopenharmony_ci} 463e41f4b71Sopenharmony_ci``` 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci## hiAppEvent.setUserProperty<sup>11+</sup> 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_cisetUserProperty(name: string, value: string): void 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci设置用户属性。 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci**参数:** 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 478e41f4b71Sopenharmony_ci| --------- | ------------------------- | ---- | -------------- | 479e41f4b71Sopenharmony_ci| name | string | 是 | 用户属性的key。只能包含大小写字母、数字、下划线和 $,不能以数字开头,长度非空且不超过256个字符。 | 480e41f4b71Sopenharmony_ci| value | string | 是 | 用户属性的值。长度不超过1024,当值为null、undefine或空,则清除用户属性。 | 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci**错误码:** 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 485e41f4b71Sopenharmony_ci| ------- | ----------------- | 486e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci**示例:** 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci```ts 491e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_citry { 494e41f4b71Sopenharmony_ci hiAppEvent.setUserProperty('key', 'value'); 495e41f4b71Sopenharmony_ci} catch (error) { 496e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to setUserProperty event, code=${error.code}`); 497e41f4b71Sopenharmony_ci} 498e41f4b71Sopenharmony_ci``` 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci## hiAppEvent.getUserProperty<sup>11+</sup> 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_cigetUserProperty(name: string): string 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci获取之前通过setUserProperty接口设置的value值。 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci**参数:** 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 513e41f4b71Sopenharmony_ci| --------- | ----------------------- | ---- | ---------- | 514e41f4b71Sopenharmony_ci| name | string | 是 | 用户属性的key。只能包含大小写字母、数字、下划线和 $,不能以数字开头,长度不超过256。| 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci**返回值:** 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci| 类型 | 说明 | 519e41f4b71Sopenharmony_ci| ------ | -------------------------------- | 520e41f4b71Sopenharmony_ci| string | 用户属性的值。 没有查到返回空字符串。 | 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci**错误码:** 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 525e41f4b71Sopenharmony_ci| ------- | ----------------- | 526e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci**示例:** 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ci```ts 531e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_cihiAppEvent.setUserProperty('key', 'value'); 534e41f4b71Sopenharmony_citry { 535e41f4b71Sopenharmony_ci let value: string = hiAppEvent.getUserProperty('key'); 536e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `getUserProperty event was successful, userProperty=${value}`); 537e41f4b71Sopenharmony_ci} catch (error) { 538e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', `failed to getUserProperty event, code=${error.code}`); 539e41f4b71Sopenharmony_ci} 540e41f4b71Sopenharmony_ci``` 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci## hiAppEvent.addWatcher 543e41f4b71Sopenharmony_ci 544e41f4b71Sopenharmony_ciaddWatcher(watcher: Watcher): AppEventPackageHolder 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci添加应用事件观察者方法,可用于订阅应用事件。 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ci**参数:** 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 555e41f4b71Sopenharmony_ci| ------- | -------------------- | ---- | ---------------- | 556e41f4b71Sopenharmony_ci| watcher | [Watcher](#watcher) | 是 | 应用事件观察者。 | 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci**返回值:** 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci| 类型 | 说明 | 561e41f4b71Sopenharmony_ci| ------------------------------------------------ | ------------------------------------ | 562e41f4b71Sopenharmony_ci| [AppEventPackageHolder](#appeventpackageholder) | 订阅数据持有者,订阅失败时返回null。 | 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci**错误码:** 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 569e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 570e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 571e41f4b71Sopenharmony_ci| 11102001 | Invalid watcher name. | 572e41f4b71Sopenharmony_ci| 11102002 | Invalid filtering event domain. | 573e41f4b71Sopenharmony_ci| 11102003 | Invalid row value. | 574e41f4b71Sopenharmony_ci| 11102004 | Invalid size value. | 575e41f4b71Sopenharmony_ci| 11102005 | Invalid timeout value. | 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**示例:** 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci```ts 580e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_ci// 1. 如果观察者传入了回调的相关参数,则可以选择在自动触发的回调函数中对订阅事件进行处理 583e41f4b71Sopenharmony_cihiAppEvent.addWatcher({ 584e41f4b71Sopenharmony_ci name: "watcher1", 585e41f4b71Sopenharmony_ci appEventFilters: [ 586e41f4b71Sopenharmony_ci { 587e41f4b71Sopenharmony_ci domain: "test_domain", 588e41f4b71Sopenharmony_ci eventTypes: [hiAppEvent.EventType.FAULT, hiAppEvent.EventType.BEHAVIOR] 589e41f4b71Sopenharmony_ci } 590e41f4b71Sopenharmony_ci ], 591e41f4b71Sopenharmony_ci triggerCondition: { 592e41f4b71Sopenharmony_ci row: 10, 593e41f4b71Sopenharmony_ci size: 1000, 594e41f4b71Sopenharmony_ci timeOut: 1 595e41f4b71Sopenharmony_ci }, 596e41f4b71Sopenharmony_ci onTrigger: (curRow: number, curSize: number, holder: hiAppEvent.AppEventPackageHolder) => { 597e41f4b71Sopenharmony_ci if (holder == null) { 598e41f4b71Sopenharmony_ci hilog.error(0x0000, 'hiAppEvent', "holder is null"); 599e41f4b71Sopenharmony_ci return; 600e41f4b71Sopenharmony_ci } 601e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `curRow=${curRow}, curSize=${curSize}`); 602e41f4b71Sopenharmony_ci let eventPkg: hiAppEvent.AppEventPackage | null = null; 603e41f4b71Sopenharmony_ci while ((eventPkg = holder.takeNext()) != null) { 604e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.packageId=${eventPkg.packageId}`); 605e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.row=${eventPkg.row}`); 606e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.size=${eventPkg.size}`); 607e41f4b71Sopenharmony_ci for (const eventInfo of eventPkg.data) { 608e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.data=${eventInfo}`); 609e41f4b71Sopenharmony_ci } 610e41f4b71Sopenharmony_ci } 611e41f4b71Sopenharmony_ci } 612e41f4b71Sopenharmony_ci}); 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ci// 2. 如果观察者未传入回调的相关参数,则可以选择使用返回的holder对象手动去处理订阅事件 615e41f4b71Sopenharmony_ci// 针对异常退出时产生的崩溃事件(hiAppEvent.event.APP_CRASH)和卡死事件(hiAppEvent.event.APP_FREEZE),系统捕获维测日志有一定耗时,典型情况下30s内完成,极端情况下2min左右完成。在手动处理订阅事件的方法中,建议在进程启动后延时重试调用takeNext()获取此类事件。 616e41f4b71Sopenharmony_cilet holder = hiAppEvent.addWatcher({ 617e41f4b71Sopenharmony_ci name: "watcher2", 618e41f4b71Sopenharmony_ci}); 619e41f4b71Sopenharmony_ciif (holder != null) { 620e41f4b71Sopenharmony_ci let eventPkg: hiAppEvent.AppEventPackage | null = null; 621e41f4b71Sopenharmony_ci while ((eventPkg = holder.takeNext()) != null) { 622e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.packageId=${eventPkg.packageId}`); 623e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.row=${eventPkg.row}`); 624e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.size=${eventPkg.size}`); 625e41f4b71Sopenharmony_ci for (const eventInfo of eventPkg.data) { 626e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventPkg.data=${eventInfo}`); 627e41f4b71Sopenharmony_ci } 628e41f4b71Sopenharmony_ci } 629e41f4b71Sopenharmony_ci} 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci// 3. 观察者可以在实时回调函数onReceive中处理订阅事件 632e41f4b71Sopenharmony_cihiAppEvent.addWatcher({ 633e41f4b71Sopenharmony_ci name: "watcher3", 634e41f4b71Sopenharmony_ci appEventFilters: [ 635e41f4b71Sopenharmony_ci { 636e41f4b71Sopenharmony_ci domain: "test_domain", 637e41f4b71Sopenharmony_ci eventTypes: [hiAppEvent.EventType.FAULT, hiAppEvent.EventType.BEHAVIOR] 638e41f4b71Sopenharmony_ci } 639e41f4b71Sopenharmony_ci ], 640e41f4b71Sopenharmony_ci onReceive: (domain: string, appEventGroups: Array<hiAppEvent.AppEventGroup>) => { 641e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `domain=${domain}`); 642e41f4b71Sopenharmony_ci for (const eventGroup of appEventGroups) { 643e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `eventName=${eventGroup.name}`); 644e41f4b71Sopenharmony_ci for (const eventInfo of eventGroup.appEventInfos) { 645e41f4b71Sopenharmony_ci hilog.info(0x0000, 'hiAppEvent', `event=${JSON.stringify(eventInfo)}`, ); 646e41f4b71Sopenharmony_ci } 647e41f4b71Sopenharmony_ci } 648e41f4b71Sopenharmony_ci } 649e41f4b71Sopenharmony_ci}); 650e41f4b71Sopenharmony_ci``` 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci## hiAppEvent.removeWatcher 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ciremoveWatcher(watcher: Watcher): void 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ci移除应用事件观察者方法,可用于取消订阅应用事件。 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ci**参数:** 663e41f4b71Sopenharmony_ci 664e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 665e41f4b71Sopenharmony_ci| ------- | -------------------- | ---- | ---------------- | 666e41f4b71Sopenharmony_ci| watcher | [Watcher](#watcher) | 是 | 应用事件观察者。 | 667e41f4b71Sopenharmony_ci 668e41f4b71Sopenharmony_ci**错误码:** 669e41f4b71Sopenharmony_ci 670e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 673e41f4b71Sopenharmony_ci| -------- | --------------------- | 674e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 675e41f4b71Sopenharmony_ci| 11102001 | Invalid watcher name. | 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci**示例:** 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci```ts 680e41f4b71Sopenharmony_ci// 1. 定义一个应用事件观察者 681e41f4b71Sopenharmony_cilet watcher: hiAppEvent.Watcher = { 682e41f4b71Sopenharmony_ci name: "watcher1", 683e41f4b71Sopenharmony_ci} 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci// 2. 添加一个应用事件观察者来订阅事件 686e41f4b71Sopenharmony_cihiAppEvent.addWatcher(watcher); 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci// 3. 移除该应用事件观察者以取消订阅事件 689e41f4b71Sopenharmony_cihiAppEvent.removeWatcher(watcher); 690e41f4b71Sopenharmony_ci``` 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ci## Watcher 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci提供了应用事件观察者的参数选项。 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 697e41f4b71Sopenharmony_ci 698e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 701e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 702e41f4b71Sopenharmony_ci| name | string | 是 | 观察者名称,用于唯一标识观察者。 | 703e41f4b71Sopenharmony_ci| triggerCondition | [TriggerCondition](#triggercondition) | 否 | 订阅回调触发条件,需要与回调函数onTrigger一同传入才会生效。 | 704e41f4b71Sopenharmony_ci| appEventFilters | [AppEventFilter](#appeventfilter)[] | 否 | 订阅过滤条件,在需要对订阅事件进行过滤时传入。 | 705e41f4b71Sopenharmony_ci| onTrigger | (curRow: number, curSize: number, holder: [AppEventPackageHolder](#appeventpackageholder)) => void | 否 | 订阅回调函数,需要与回调触发条件triggerCondition一同传入才会生效,函数入参说明如下:<br>curRow:在本次回调触发时的订阅事件总数量; <br>curSize:在本次回调触发时的订阅事件总大小,单位为byte; <br/>holder:订阅数据持有者对象,可以通过其对订阅事件进行处理。 | 706e41f4b71Sopenharmony_ci| onReceive<sup>11+</sup> | (domain: string, appEventGroups: Array<[AppEventGroup](#appeventgroup11)>) => void | 否 | 订阅实时回调函数,与回调函数onTrigger同时存在时,只触发此回调,函数入参说明如下:<br>domain:回调事件的领域名称; <br>appEventGroups:回调事件集合。 | 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ci## TriggerCondition 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ci提供了回调触发条件的参数选项,只要满足任一条件就会触发订阅回调。 711e41f4b71Sopenharmony_ci 712e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 713e41f4b71Sopenharmony_ci 714e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 715e41f4b71Sopenharmony_ci 716e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 717e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -------------------------------------- | 718e41f4b71Sopenharmony_ci| row | number | 否 | 满足触发回调的事件总数量,正整数。默认值0,不触发回调。传入负值时,会被置为默认值。 | 719e41f4b71Sopenharmony_ci| size | number | 否 | 满足触发回调的事件总大小,正整数,单位为byte。默认值0,不触发回调。传入负值时,会被置为默认值。 | 720e41f4b71Sopenharmony_ci| timeOut | number | 否 | 满足触发回调的超时时长,正整数,单位为30s。默认值0,不触发回调。传入负值时,会被置为默认值。 | 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci## AppEventFilter 723e41f4b71Sopenharmony_ci 724e41f4b71Sopenharmony_ci提供了过滤应用事件的参数选项。 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 731e41f4b71Sopenharmony_ci| ---------- | ------------------------- | ---- | ------------------------ | 732e41f4b71Sopenharmony_ci| domain | string | 是 | 需要订阅的事件领域。 | 733e41f4b71Sopenharmony_ci| eventTypes | [EventType](#eventtype)[] | 否 | 需要订阅的事件类型集合。 | 734e41f4b71Sopenharmony_ci| names<sup>11+</sup> | string[] | 否 | 需要订阅的事件名称集合。 | 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci## AppEventPackageHolder 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ci订阅数据持有者类,用于对订阅事件进行处理。 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci### constructor 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ciconstructor(watcherName: string) 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci类构造函数,创建订阅数据持有者实例,通过观察者名称关联到应用内已添加的观察者对象。 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci**参数:** 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 753e41f4b71Sopenharmony_ci| ------ | ----------------- | ---- | ------------------------ | 754e41f4b71Sopenharmony_ci| watcherName | string | 是 | 观察者名称。 | 755e41f4b71Sopenharmony_ci 756e41f4b71Sopenharmony_ci**示例:** 757e41f4b71Sopenharmony_ci 758e41f4b71Sopenharmony_ci```ts 759e41f4b71Sopenharmony_cilet holder1: hiAppEvent.AppEventPackageHolder = new hiAppEvent.AppEventPackageHolder("watcher1"); 760e41f4b71Sopenharmony_ci``` 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ci### setSize 763e41f4b71Sopenharmony_ci 764e41f4b71Sopenharmony_cisetSize(size: number): void 765e41f4b71Sopenharmony_ci 766e41f4b71Sopenharmony_ci设置每次取出的应用事件包的数据大小阈值。 767e41f4b71Sopenharmony_ci 768e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 769e41f4b71Sopenharmony_ci 770e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 771e41f4b71Sopenharmony_ci 772e41f4b71Sopenharmony_ci**参数:** 773e41f4b71Sopenharmony_ci 774e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 775e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- | 776e41f4b71Sopenharmony_ci| size | number | 是 | 数据大小阈值,单位为byte,取值范围是大于等于0的数,超出范围会抛异常。 | 777e41f4b71Sopenharmony_ci 778e41f4b71Sopenharmony_ci**错误码:** 779e41f4b71Sopenharmony_ci 780e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 781e41f4b71Sopenharmony_ci 782e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 783e41f4b71Sopenharmony_ci| -------- | ------------------- | 784e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 785e41f4b71Sopenharmony_ci| 11104001 | Invalid size value. | 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci**示例:** 788e41f4b71Sopenharmony_ci 789e41f4b71Sopenharmony_ci```ts 790e41f4b71Sopenharmony_cilet holder2: hiAppEvent.AppEventPackageHolder = new hiAppEvent.AppEventPackageHolder("watcher2"); 791e41f4b71Sopenharmony_ciholder2.setSize(1000); 792e41f4b71Sopenharmony_ci``` 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci### setRow<sup>12+</sup> 795e41f4b71Sopenharmony_ci 796e41f4b71Sopenharmony_cisetRow(size: number): void 797e41f4b71Sopenharmony_ci 798e41f4b71Sopenharmony_ci设置每次取出的应用事件包的数据条数,优先级高于setSize,和setSize同时调用时仅setRow生效。 799e41f4b71Sopenharmony_ci 800e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 801e41f4b71Sopenharmony_ci 802e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci**参数:** 805e41f4b71Sopenharmony_ci 806e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 807e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------------- | 808e41f4b71Sopenharmony_ci| size | number | 是 | 应用事件条数,单位为条,取值范围是大于0的数,超出范围会抛异常。 | 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci**错误码:** 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[应用事件打点错误码](errorcode-hiappevent.md)。 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 815e41f4b71Sopenharmony_ci| -------- | ------------------- | 816e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 817e41f4b71Sopenharmony_ci| 11104001 | Invalid size value. | 818e41f4b71Sopenharmony_ci 819e41f4b71Sopenharmony_ci**示例:** 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci```ts 822e41f4b71Sopenharmony_cilet holder3: hiAppEvent.AppEventPackageHolder = new hiAppEvent.AppEventPackageHolder("watcher3"); 823e41f4b71Sopenharmony_ciholder3.setRow(1000); 824e41f4b71Sopenharmony_ci``` 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ci### takeNext 827e41f4b71Sopenharmony_ci 828e41f4b71Sopenharmony_citakeNext(): AppEventPackage 829e41f4b71Sopenharmony_ci 830e41f4b71Sopenharmony_ci根据设置的数据大小阈值或条数来取出订阅事件数据,当订阅事件数据全部被取出时返回null作为标识。 831e41f4b71Sopenharmony_ci1、应用仅调用setSize不调用setRow时,根据数据大小限制取订阅事件。 832e41f4b71Sopenharmony_ci2、应用调用setRow,无论是否调用setSize,都根据setRow设置的条数取订阅事件。 833e41f4b71Sopenharmony_ci3、setSize和setRow都没被调用时,默认取1条订阅事件。 834e41f4b71Sopenharmony_ci 835e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci**返回值:** 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ci| 类型 | 说明 | 842e41f4b71Sopenharmony_ci| ----------------------------------- | ------------------------------------------------------ | 843e41f4b71Sopenharmony_ci| [AppEventPackage](#appeventpackage) | 取出的事件包对象,订阅事件数据被全部取出后会返回null。 | 844e41f4b71Sopenharmony_ci 845e41f4b71Sopenharmony_ci**示例:** 846e41f4b71Sopenharmony_ci 847e41f4b71Sopenharmony_ci```ts 848e41f4b71Sopenharmony_cilet holder4: hiAppEvent.AppEventPackageHolder = new hiAppEvent.AppEventPackageHolder("watcher4"); 849e41f4b71Sopenharmony_cilet eventPkg = holder4.takeNext(); 850e41f4b71Sopenharmony_ci``` 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ci## AppEventPackage 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci提供了订阅返回的应用事件包的参数定义。 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 859e41f4b71Sopenharmony_ci| --------- | -------- | ---- | ------------------------------ | 860e41f4b71Sopenharmony_ci| packageId | number | 是 | 事件包ID,从0开始自动递增。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 861e41f4b71Sopenharmony_ci| row | number | 是 | 事件包的事件数量。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 862e41f4b71Sopenharmony_ci| size | number | 是 | 事件包的事件大小,单位为byte。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 863e41f4b71Sopenharmony_ci| data | string[] | 是 | 事件包的事件信息。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 864e41f4b71Sopenharmony_ci| appEventInfos<sup>12+</sup> | Array<[AppEventInfo](#appeventinfo)> | 是 | 事件对象集合。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 865e41f4b71Sopenharmony_ci 866e41f4b71Sopenharmony_ci## AppEventGroup<sup>11+</sup> 867e41f4b71Sopenharmony_ci 868e41f4b71Sopenharmony_ci提供了订阅返回的事件组的参数定义。 869e41f4b71Sopenharmony_ci 870e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 871e41f4b71Sopenharmony_ci 872e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 873e41f4b71Sopenharmony_ci 874e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 875e41f4b71Sopenharmony_ci| ------------- | ------------------------------- | ---- | ------------- | 876e41f4b71Sopenharmony_ci| name | string | 是 | 事件名称。 | 877e41f4b71Sopenharmony_ci| appEventInfos | Array<[AppEventInfo](#appeventinfo)> | 是 | 事件对象集合。 | 878e41f4b71Sopenharmony_ci 879e41f4b71Sopenharmony_ci## hiAppEvent.clearData 880e41f4b71Sopenharmony_ci 881e41f4b71Sopenharmony_ciclearData(): void 882e41f4b71Sopenharmony_ci 883e41f4b71Sopenharmony_ci应用事件打点数据清理方法,将应用存储在本地的打点数据进行清除。 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 886e41f4b71Sopenharmony_ci 887e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 888e41f4b71Sopenharmony_ci 889e41f4b71Sopenharmony_ci**示例:** 890e41f4b71Sopenharmony_ci 891e41f4b71Sopenharmony_ci```ts 892e41f4b71Sopenharmony_cihiAppEvent.clearData(); 893e41f4b71Sopenharmony_ci``` 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci 896e41f4b71Sopenharmony_ci## EventType 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ci事件类型枚举。 899e41f4b71Sopenharmony_ci 900e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 901e41f4b71Sopenharmony_ci 902e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 905e41f4b71Sopenharmony_ci| --------- | ---- | -------------- | 906e41f4b71Sopenharmony_ci| FAULT | 1 | 故障类型事件。 | 907e41f4b71Sopenharmony_ci| STATISTIC | 2 | 统计类型事件。 | 908e41f4b71Sopenharmony_ci| SECURITY | 3 | 安全类型事件。 | 909e41f4b71Sopenharmony_ci| BEHAVIOR | 4 | 行为类型事件。 | 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci 912e41f4b71Sopenharmony_ci## domain<sup>11+</sup> 913e41f4b71Sopenharmony_ci 914e41f4b71Sopenharmony_ci提供了所有预定义事件的领域名称常量。 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ci| 名称 | 类型 | 说明 | 921e41f4b71Sopenharmony_ci| --- | ------ | ---------- | 922e41f4b71Sopenharmony_ci| OS | string | 系统领域。 | 923e41f4b71Sopenharmony_ci 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci## event 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ci提供了所有预定义事件的事件名称常量。 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ci| 名称 | 类型 | 说明 | 932e41f4b71Sopenharmony_ci| ------------------------- | ------ | -------------------- | 933e41f4b71Sopenharmony_ci| USER_LOGIN | string | 用户登录事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 934e41f4b71Sopenharmony_ci| USER_LOGOUT | string | 用户登出事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 935e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_START | string | 分布式服务启动事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 936e41f4b71Sopenharmony_ci| APP_CRASH<sup>11+</sup> | string | 应用崩溃事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 937e41f4b71Sopenharmony_ci| APP_FREEZE<sup>11+</sup> | string | 应用卡死事件。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 938e41f4b71Sopenharmony_ci| APP_LAUNCH<sup>12+</sup> | string | 应用启动耗时事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 939e41f4b71Sopenharmony_ci| SCROLL_JANK<sup>12+</sup> | string | 应用滑动丢帧事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 940e41f4b71Sopenharmony_ci| CPU_USAGE_HIGH<sup>12+</sup> | string | 应用CPU高负载事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 941e41f4b71Sopenharmony_ci| BATTERY_USAGE<sup>12+</sup> | string | 应用24h功耗器件分解统计事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 942e41f4b71Sopenharmony_ci| RESOURCE_OVERLIMIT<sup>12+</sup> | string | 应用资源泄露事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 943e41f4b71Sopenharmony_ci| ADDRESS_SANITIZER<sup>12+</sup> | string | 应用踩内存事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 944e41f4b71Sopenharmony_ci| MAIN_THREAD_JANK<sup>12+</sup> | string | 应用主线程超时事件。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 945e41f4b71Sopenharmony_ci 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_ci## param 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci提供了所有预定义参数的参数名称常量。 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.HiviewDFX.HiAppEvent 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci| 名称 | 类型 | 说明 | 956e41f4b71Sopenharmony_ci| ------------------------------- | ------ | ------------------ | 957e41f4b71Sopenharmony_ci| USER_ID | string | 用户自定义ID。 | 958e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_NAME | string | 分布式服务名称。 | 959e41f4b71Sopenharmony_ci| DISTRIBUTED_SERVICE_INSTANCE_ID | string | 分布式服务实例ID。 |