1e41f4b71Sopenharmony_ci# @ohos.app.ability.UIAbility (UIAbility) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciUIAbility是包含UI界面的应用组件,继承自[Ability](js-apis-app-ability-ability.md),提供组件创建、销毁、前后台切换等生命周期回调,同时也具备组件协同的能力,组件协同主要提供如下常用功能: 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- [Caller](#caller):由[startAbilityByCall](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilitybycall)接口返回,CallerAbility(调用者)可使用Caller与CalleeAbility(被调用者)进行通信。 6e41f4b71Sopenharmony_ci- [Callee](#callee):UIAbility的内部对象,CalleeAbility(被调用者)可以通过Callee与Caller进行通信。 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci各类Ability的继承关系详见[继承关系说明](./js-apis-app-ability-ability.md#ability的继承关系说明)。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci> **说明:** 11e41f4b71Sopenharmony_ci> 12e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13e41f4b71Sopenharmony_ci> 14e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## 导入模块 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci```ts 19e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 20e41f4b71Sopenharmony_ci``` 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## 属性 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 27e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 28e41f4b71Sopenharmony_ci| context | [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md) | 否 | 否 | 上下文。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 29e41f4b71Sopenharmony_ci| launchWant | [Want](js-apis-app-ability-want.md) | 否 | 否 | UIAbility启动时的参数。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 30e41f4b71Sopenharmony_ci| lastRequestWant | [Want](js-apis-app-ability-want.md) | 否 | 否 | UIAbility最后请求时的参数。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 31e41f4b71Sopenharmony_ci| callee | [Callee](#callee) | 否 | 否 | 调用Stub(桩)服务对象。| 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci## UIAbility.onCreate 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_cionCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciUIAbility实例处于完全关闭状态下被创建完成后进入该生命周期回调,执行初始化业务逻辑操作。即UIAbility实例[冷启动](../../application-models/uiability-intra-device-interaction.md#目标uiability冷启动)时进入该生命周期回调。同步接口,不支持异步回调。 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**参数:** 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 46e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 47e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 当前UIAbility的Want类型信息,包括ability名称、bundle名称等。 | 48e41f4b71Sopenharmony_ci| launchParam | [AbilityConstant.LaunchParam](js-apis-app-ability-abilityConstant.md#launchparam) | 是 | 创建 ability、上次异常退出的原因信息。 | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**示例:** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```ts 53e41f4b71Sopenharmony_ciimport { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 56e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 57e41f4b71Sopenharmony_ci console.log(`onCreate, want: ${want.abilityName}`); 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci} 60e41f4b71Sopenharmony_ci``` 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci## UIAbility.onWindowStageCreate 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_cionWindowStageCreate(windowStage: window.WindowStage): void 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci当WindowStage创建后调用。 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**参数:** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 76e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 77e41f4b71Sopenharmony_ci| windowStage | [window.WindowStage](../apis-arkui/js-apis-window.md#windowstage9) | 是 | WindowStage相关信息。 | 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**示例:** 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci```ts 82e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 83e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 86e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 87e41f4b71Sopenharmony_ci console.log('onWindowStageCreate'); 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci``` 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci## UIAbility.onWindowStageWillDestroy<sup>12+</sup> 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_cionWindowStageWillDestroy(windowStage: window.WindowStage): void 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci当WindowStage即将销毁时调用。 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**参数:** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 106e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 107e41f4b71Sopenharmony_ci| windowStage | [window.WindowStage](../apis-arkui/js-apis-window.md#windowstage9) | 是 | WindowStage相关信息。 | 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**示例:** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci```ts 112e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 113e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 116e41f4b71Sopenharmony_ci onWindowStageWillDestroy(windowStage: window.WindowStage) { 117e41f4b71Sopenharmony_ci console.log('onWindowStageWillDestroy'); 118e41f4b71Sopenharmony_ci } 119e41f4b71Sopenharmony_ci} 120e41f4b71Sopenharmony_ci``` 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci## UIAbility.onWindowStageDestroy 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_cionWindowStageDestroy(): void 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci当WindowStage销毁后调用。 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**示例:** 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci```ts 136e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 139e41f4b71Sopenharmony_ci onWindowStageDestroy() { 140e41f4b71Sopenharmony_ci console.log('onWindowStageDestroy'); 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci} 143e41f4b71Sopenharmony_ci``` 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci## UIAbility.onWindowStageRestore 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_cionWindowStageRestore(windowStage: window.WindowStage): void 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci当迁移多实例ability时,恢复WindowStage后调用。 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci**参数:** 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 159e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 160e41f4b71Sopenharmony_ci| windowStage | [window.WindowStage](../apis-arkui/js-apis-window.md#windowstage9) | 是 | WindowStage相关信息。 | 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci**示例:** 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci```ts 165e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 166e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 169e41f4b71Sopenharmony_ci onWindowStageRestore(windowStage: window.WindowStage) { 170e41f4b71Sopenharmony_ci console.log('onWindowStageRestore'); 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci} 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci## UIAbility.onDestroy 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_cionDestroy(): void | Promise<void> 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ciUIAbility生命周期回调,在销毁时回调,执行资源清理等操作。使用同步回调或Promise异步回调。 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**返回值:** 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci| 类型 | 说明 | 189e41f4b71Sopenharmony_ci| -------- | -------- | 190e41f4b71Sopenharmony_ci| void \| Promise<void> | 无返回结果或无返回结果的Promise对象。 | 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci**示例:** 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci```ts 195e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 198e41f4b71Sopenharmony_ci onDestroy() { 199e41f4b71Sopenharmony_ci console.log('onDestroy'); 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci在执行完onDestroy生命周期回调后,应用可能会退出,从而可能导致onDestroy中的异步函数未能正确执行,比如异步写入数据库。可以使用异步生命周期,以确保异步onDestroy完成后再继续后续的生命周期。 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci```ts 207e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 210e41f4b71Sopenharmony_ci async onDestroy() { 211e41f4b71Sopenharmony_ci console.log('onDestroy'); 212e41f4b71Sopenharmony_ci // 调用异步函数... 213e41f4b71Sopenharmony_ci } 214e41f4b71Sopenharmony_ci} 215e41f4b71Sopenharmony_ci``` 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci## UIAbility.onForeground 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_cionForeground(): void 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ciUIAbility生命周期回调,当应用从后台转到前台时触发。同步接口,不支持异步回调。 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci**示例:** 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci```ts 230e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 233e41f4b71Sopenharmony_ci onForeground() { 234e41f4b71Sopenharmony_ci console.log('onForeground'); 235e41f4b71Sopenharmony_ci } 236e41f4b71Sopenharmony_ci} 237e41f4b71Sopenharmony_ci``` 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci## UIAbility.onBackground 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_cionBackground(): void 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ciUIAbility生命周期回调,当应用从前台转到后台时触发。同步接口,不支持异步回调。 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci**示例:** 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci```ts 253e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 256e41f4b71Sopenharmony_ci onBackground() { 257e41f4b71Sopenharmony_ci console.log('onBackground'); 258e41f4b71Sopenharmony_ci } 259e41f4b71Sopenharmony_ci} 260e41f4b71Sopenharmony_ci``` 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci## UIAbility.onContinue 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_cionContinue(wantParam: Record<string, Object>): AbilityConstant.OnContinueResult | Promise<AbilityConstant.OnContinueResult> 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci当Ability准备迁移时触发,保存数据。 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**说明:** 270e41f4b71Sopenharmony_ci> 271e41f4b71Sopenharmony_ci> 从API version 12 开始,UIAbility.onContinue生命周期新增支持返回值为Promise\<[AbilityConstant.OnContinueResult](js-apis-app-ability-abilityConstant.md#oncontinueresult)\>形式。 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci**参数:** 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 280e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 281e41f4b71Sopenharmony_ci| wantParam | Record<string, Object> | 是 | want相关参数。 | 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci**返回值:** 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci| 类型 | 说明 | 286e41f4b71Sopenharmony_ci| -------- | -------- | 287e41f4b71Sopenharmony_ci| [AbilityConstant.OnContinueResult](js-apis-app-ability-abilityConstant.md#oncontinueresult) \| Promise<[AbilityConstant.OnContinueResult](js-apis-app-ability-abilityConstant.md#oncontinueresult)> | 接续的结果或带接续结果的Promise对象。 | 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci**示例:** 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci ```ts 292e41f4b71Sopenharmony_ci import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci class MyUIAbility extends UIAbility { 295e41f4b71Sopenharmony_ci onContinue(wantParams: Record<string, Object>) { 296e41f4b71Sopenharmony_ci console.log('onContinue'); 297e41f4b71Sopenharmony_ci wantParams['myData'] = 'my1234567'; 298e41f4b71Sopenharmony_ci return AbilityConstant.OnContinueResult.AGREE; 299e41f4b71Sopenharmony_ci } 300e41f4b71Sopenharmony_ci } 301e41f4b71Sopenharmony_ci ``` 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci支持应用在迁移时,使用异步接口进行数据保存。 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci ```ts 306e41f4b71Sopenharmony_ci import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci class MyUIAbility extends UIAbility { 309e41f4b71Sopenharmony_ci async setWant(wantParams: Record<string, Object>) { 310e41f4b71Sopenharmony_ci console.log('setWant start'); 311e41f4b71Sopenharmony_ci for (let time = 0; time < 1000; ++time) { 312e41f4b71Sopenharmony_ci wantParams[time] = time; 313e41f4b71Sopenharmony_ci } 314e41f4b71Sopenharmony_ci console.log('setWant end'); 315e41f4b71Sopenharmony_ci } 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci async onContinue(wantParams: Record<string, Object>) { 318e41f4b71Sopenharmony_ci console.log('onContinue'); 319e41f4b71Sopenharmony_ci return this.setWant(wantParams).then(()=>{ 320e41f4b71Sopenharmony_ci return AbilityConstant.OnContinueResult.AGREE; 321e41f4b71Sopenharmony_ci }); 322e41f4b71Sopenharmony_ci } 323e41f4b71Sopenharmony_ci } 324e41f4b71Sopenharmony_ci ``` 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci## UIAbility.onNewWant 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_cionNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ciUIAbility实例已经启动并在前台运行过,由于某些原因切换到后台,再次启动该UIAbility实例时会回调执行该方法。即UIAbility实例[热启动](../../application-models/uiability-intra-device-interaction.md#目标uiability热启动)时进入该生命周期回调。 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**参数:** 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 340e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 341e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | Want类型参数,如ability名称,包名等。 | 342e41f4b71Sopenharmony_ci| launchParam | [AbilityConstant.LaunchParam](js-apis-app-ability-abilityConstant.md#launchparam) | 是 | UIAbility启动的原因、上次异常退出的原因信息。 | 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**示例:** 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci```ts 347e41f4b71Sopenharmony_ciimport { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 350e41f4b71Sopenharmony_ci onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 351e41f4b71Sopenharmony_ci console.log(`onNewWant, want: ${want.abilityName}`); 352e41f4b71Sopenharmony_ci console.log(`onNewWant, launchParam: ${JSON.stringify(launchParam)}`); 353e41f4b71Sopenharmony_ci } 354e41f4b71Sopenharmony_ci} 355e41f4b71Sopenharmony_ci``` 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci## UIAbility.onDump 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_cionDump(params: Array\<string>): Array\<string> 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci转储客户端信息时调用,可用于转储非敏感信息。 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci**参数:** 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 370e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 371e41f4b71Sopenharmony_ci| params | Array\<string> | 是 | 表示命令形式的参数。| 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci**返回值:** 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci| 类型 | 说明 | 376e41f4b71Sopenharmony_ci| -------- | -------- | 377e41f4b71Sopenharmony_ci| Array\<string> | 转储信息数组。| 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci**示例:** 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci```ts 382e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 385e41f4b71Sopenharmony_ci onDump(params: Array<string>) { 386e41f4b71Sopenharmony_ci console.log(`dump, params: ${JSON.stringify(params)}`); 387e41f4b71Sopenharmony_ci return ['params']; 388e41f4b71Sopenharmony_ci } 389e41f4b71Sopenharmony_ci} 390e41f4b71Sopenharmony_ci``` 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci## UIAbility.onSaveState 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_cionSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>): AbilityConstant.OnSaveResult 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci该API配合[appRecovery](js-apis-app-ability-appRecovery.md)使用。在应用故障时,如果使能了自动保存状态,框架将回调onSaveState保存UIAbility状态。 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci**参数:** 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 406e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 407e41f4b71Sopenharmony_ci| reason | [AbilityConstant.StateType](js-apis-app-ability-abilityConstant.md#statetype) | 是 | 回调保存状态的原因。 | 408e41f4b71Sopenharmony_ci| wantParam | Record<string, Object> | 是 | want相关参数。 | 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci**返回值:** 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci| 类型 | 说明 | 413e41f4b71Sopenharmony_ci| -------- | -------- | 414e41f4b71Sopenharmony_ci| [AbilityConstant.OnSaveResult](js-apis-app-ability-abilityConstant.md#onsaveresult) | 是否同意保存当前UIAbility的状态。 | 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci**示例:** 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci```ts 419e41f4b71Sopenharmony_ciimport { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 422e41f4b71Sopenharmony_ci onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) { 423e41f4b71Sopenharmony_ci console.log('onSaveState'); 424e41f4b71Sopenharmony_ci wantParam['myData'] = 'my1234567'; 425e41f4b71Sopenharmony_ci return AbilityConstant.OnSaveResult.RECOVERY_AGREE; 426e41f4b71Sopenharmony_ci } 427e41f4b71Sopenharmony_ci} 428e41f4b71Sopenharmony_ci``` 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci## UIAbility.onShare<sup>10+</sup> 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_cionShare(wantParam: Record<string, Object>): void 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci在跨端分享场景下,在UIAbility中设置分享方设备要分享的数据。 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci**参数:** 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 443e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 444e41f4b71Sopenharmony_ci| wantParam | Record<string, Object> | 是 | 待分享的数据。 | 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci**示例:** 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci```ts 449e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ciclass MyUIAbility extends UIAbility { 452e41f4b71Sopenharmony_ci onShare(wantParams: Record<string, Object>) { 453e41f4b71Sopenharmony_ci console.log('onShare'); 454e41f4b71Sopenharmony_ci wantParams['ohos.extra.param.key.shareUrl'] = 'example.com'; 455e41f4b71Sopenharmony_ci } 456e41f4b71Sopenharmony_ci} 457e41f4b71Sopenharmony_ci``` 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci## UIAbility.onPrepareToTerminate<sup>10+</sup> 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_cionPrepareToTerminate(): boolean 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ciUIAbility生命周期回调,当系统预关闭开关打开后(配置系统参数persist.sys.prepare_terminate为true打开),在UIAbility关闭时触发,可在回调中定义操作来决定是否继续执行关闭UIAbility的操作。如果UIAbility在退出时需要与用户交互确认是否关闭UIAbility,可在此生命周期回调中定义预关闭操作配合[terminateSelf](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself)接口退出,如弹窗确认是否关闭,并配置预关闭生命周期返回true取消正常关闭。 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.PREPARE_APP_TERMINATE 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**返回值:** 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci| 类型 | 说明 | 474e41f4b71Sopenharmony_ci| -- | -- | 475e41f4b71Sopenharmony_ci| boolean | 是否执行UIAbility关闭操作,返回true表示本次UIAbility关闭流程取消,不再退出,返回false表示UIAbility继续正常关闭。 | 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci**示例:** 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci```ts 480e41f4b71Sopenharmony_ciimport { UIAbility, Want } from '@kit.AbilityKit'; 481e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 484e41f4b71Sopenharmony_ci onPrepareToTerminate() { 485e41f4b71Sopenharmony_ci // 开发者定义预关闭动作 486e41f4b71Sopenharmony_ci // 例如拉起另一个ability,根据ability处理结果执行异步关闭 487e41f4b71Sopenharmony_ci let want: Want = { 488e41f4b71Sopenharmony_ci bundleName: "com.example.myapplication", 489e41f4b71Sopenharmony_ci moduleName: "entry", 490e41f4b71Sopenharmony_ci abilityName: "SecondAbility" 491e41f4b71Sopenharmony_ci } 492e41f4b71Sopenharmony_ci this.context.startAbilityForResult(want) 493e41f4b71Sopenharmony_ci .then((result)=>{ 494e41f4b71Sopenharmony_ci // 获取ability处理结果,当返回结果的resultCode为0关闭当前UIAbility 495e41f4b71Sopenharmony_ci console.log('startAbilityForResult success, resultCode is ' + result.resultCode); 496e41f4b71Sopenharmony_ci if (result.resultCode === 0) { 497e41f4b71Sopenharmony_ci this.context.terminateSelf(); 498e41f4b71Sopenharmony_ci } 499e41f4b71Sopenharmony_ci }).catch((err: BusinessError)=>{ 500e41f4b71Sopenharmony_ci // 异常处理 501e41f4b71Sopenharmony_ci console.error('startAbilityForResult failed, err:' + JSON.stringify(err)); 502e41f4b71Sopenharmony_ci this.context.terminateSelf(); 503e41f4b71Sopenharmony_ci }) 504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci return true; // 已定义预关闭操作后,返回true表示UIAbility取消关闭 506e41f4b71Sopenharmony_ci } 507e41f4b71Sopenharmony_ci} 508e41f4b71Sopenharmony_ci``` 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci## UIAbility.onBackPressed<sup>10+</sup> 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_cionBackPressed(): boolean 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ciUIAbility生命周期回调,当UIAbility侧滑返回时触发,根据返回值决定是否销毁UIAbility。 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci- 当targetSdkVersion<12时,默认返回值为false,会销毁UIAbility。 517e41f4b71Sopenharmony_ci- 当targetSdkVersion>=12时,默认返回值为true,会将UIAbility移动到后台不销毁。 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 522e41f4b71Sopenharmony_ci 523e41f4b71Sopenharmony_ci**返回值:** 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci| 类型 | 说明 | 526e41f4b71Sopenharmony_ci| -- | -- | 527e41f4b71Sopenharmony_ci| boolean | 返回true表示UIAbility将会被移到后台不销毁,返回false表示UIAbility将正常销毁。 | 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci**示例:** 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci```ts 532e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 535e41f4b71Sopenharmony_ci onBackPressed() { 536e41f4b71Sopenharmony_ci return true; 537e41f4b71Sopenharmony_ci } 538e41f4b71Sopenharmony_ci} 539e41f4b71Sopenharmony_ci``` 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci## Caller 542e41f4b71Sopenharmony_ci 543e41f4b71Sopenharmony_ci通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci### Caller.call 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_cicall(method: string, data: rpc.Parcelable): Promise<void> 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci向通用组件服务端发送约定序列化数据。使用Promise异步回调。 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ci**参数:** 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 556e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 557e41f4b71Sopenharmony_ci| method | string | 是 | 约定的服务端注册事件字符串。 | 558e41f4b71Sopenharmony_ci| data | [rpc.Parcelable](../apis-ipc-kit/js-apis-rpc.md#parcelable9) | 是 | 由开发者实现的Parcelable可序列化数据。 | 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**返回值:** 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci| 类型 | 说明 | 563e41f4b71Sopenharmony_ci| -------- | -------- | 564e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci**错误码:** 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 571e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 572e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 573e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 574e41f4b71Sopenharmony_ci| 16200002 | The callee does not exist. | 575e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**示例:** 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci```ts 580e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 581e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 582e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 583e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ciclass MyMessageAble implements rpc.Parcelable { // 自定义的Parcelable数据结构 586e41f4b71Sopenharmony_ci name: string 587e41f4b71Sopenharmony_ci str: string 588e41f4b71Sopenharmony_ci num: number = 1 589e41f4b71Sopenharmony_ci constructor(name: string, str: string) { 590e41f4b71Sopenharmony_ci this.name = name; 591e41f4b71Sopenharmony_ci this.str = str; 592e41f4b71Sopenharmony_ci } 593e41f4b71Sopenharmony_ci marshalling(messageSequence: rpc.MessageSequence) { 594e41f4b71Sopenharmony_ci messageSequence.writeInt(this.num); 595e41f4b71Sopenharmony_ci messageSequence.writeString(this.str); 596e41f4b71Sopenharmony_ci console.log(`MyMessageAble marshalling num[${this.num}] str[${this.str}]`); 597e41f4b71Sopenharmony_ci return true; 598e41f4b71Sopenharmony_ci } 599e41f4b71Sopenharmony_ci unmarshalling(messageSequence: rpc.MessageSequence) { 600e41f4b71Sopenharmony_ci this.num = messageSequence.readInt(); 601e41f4b71Sopenharmony_ci this.str = messageSequence.readString(); 602e41f4b71Sopenharmony_ci console.log(`MyMessageAble unmarshalling num[${this.num}] str[${this.str}]`); 603e41f4b71Sopenharmony_ci return true; 604e41f4b71Sopenharmony_ci } 605e41f4b71Sopenharmony_ci}; 606e41f4b71Sopenharmony_cilet method = 'call_Function'; // 约定的通知消息字符串 607e41f4b71Sopenharmony_cilet caller: Caller; 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 610e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 611e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 612e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 613e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 614e41f4b71Sopenharmony_ci deviceId: '' 615e41f4b71Sopenharmony_ci }).then((obj) => { 616e41f4b71Sopenharmony_ci caller = obj; 617e41f4b71Sopenharmony_ci let msg = new MyMessageAble('msg', 'world'); // 参考Parcelable数据定义 618e41f4b71Sopenharmony_ci caller.call(method, msg) 619e41f4b71Sopenharmony_ci .then(() => { 620e41f4b71Sopenharmony_ci console.log('Caller call() called'); 621e41f4b71Sopenharmony_ci }) 622e41f4b71Sopenharmony_ci .catch((callErr: BusinessError) => { 623e41f4b71Sopenharmony_ci console.error(`Caller.call catch error, error.code: ${callErr.code}, error.message: ${callErr.message}`); 624e41f4b71Sopenharmony_ci }); 625e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 626e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 627e41f4b71Sopenharmony_ci }); 628e41f4b71Sopenharmony_ci } 629e41f4b71Sopenharmony_ci} 630e41f4b71Sopenharmony_ci``` 631e41f4b71Sopenharmony_ci 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci### Caller.callWithResult 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_cicallWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence> 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci向通用组件服务端发送约定序列化数据, 并将服务端返回的约定序列化数据带回。使用Promise异步回调。 638e41f4b71Sopenharmony_ci 639e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci**参数:** 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 644e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 645e41f4b71Sopenharmony_ci| method | string | 是 | 约定的服务端注册事件字符串。 | 646e41f4b71Sopenharmony_ci| data | [rpc.Parcelable](../apis-ipc-kit/js-apis-rpc.md#parcelable9) | 是 | 由开发者实现的Parcelable可序列化数据。 | 647e41f4b71Sopenharmony_ci 648e41f4b71Sopenharmony_ci**返回值:** 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci| 类型 | 说明 | 651e41f4b71Sopenharmony_ci| -------- | -------- | 652e41f4b71Sopenharmony_ci| Promise<[rpc.MessageSequence](../apis-ipc-kit/js-apis-rpc.md#messagesequence9)> | Promise对象,返回通用组件服务端应答数据。 | 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ci**错误码:** 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 659e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 660e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 661e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 662e41f4b71Sopenharmony_ci| 16200002 | The callee does not exist. | 663e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci**示例:** 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_ci```ts 668e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 669e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 670e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 671e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ciclass MyMessageAble implements rpc.Parcelable { 674e41f4b71Sopenharmony_ci name: string 675e41f4b71Sopenharmony_ci str: string 676e41f4b71Sopenharmony_ci num: number = 1 677e41f4b71Sopenharmony_ci constructor(name: string, str: string) { 678e41f4b71Sopenharmony_ci this.name = name; 679e41f4b71Sopenharmony_ci this.str = str; 680e41f4b71Sopenharmony_ci } 681e41f4b71Sopenharmony_ci marshalling(messageSequence: rpc.MessageSequence) { 682e41f4b71Sopenharmony_ci messageSequence.writeInt(this.num); 683e41f4b71Sopenharmony_ci messageSequence.writeString(this.str); 684e41f4b71Sopenharmony_ci console.log(`MyMessageAble marshalling num[${this.num}] str[${this.str}]`); 685e41f4b71Sopenharmony_ci return true; 686e41f4b71Sopenharmony_ci } 687e41f4b71Sopenharmony_ci unmarshalling(messageSequence: rpc.MessageSequence) { 688e41f4b71Sopenharmony_ci this.num = messageSequence.readInt(); 689e41f4b71Sopenharmony_ci this.str = messageSequence.readString(); 690e41f4b71Sopenharmony_ci console.log(`MyMessageAble unmarshalling num[${this.num}] str[${this.str}]`); 691e41f4b71Sopenharmony_ci return true; 692e41f4b71Sopenharmony_ci } 693e41f4b71Sopenharmony_ci}; 694e41f4b71Sopenharmony_cilet method = 'call_Function'; 695e41f4b71Sopenharmony_cilet caller: Caller; 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 698e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 699e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 700e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 701e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 702e41f4b71Sopenharmony_ci deviceId: '' 703e41f4b71Sopenharmony_ci }).then((obj) => { 704e41f4b71Sopenharmony_ci caller = obj; 705e41f4b71Sopenharmony_ci let msg = new MyMessageAble('msg', 'world'); 706e41f4b71Sopenharmony_ci caller.callWithResult(method, msg) 707e41f4b71Sopenharmony_ci .then((data) => { 708e41f4b71Sopenharmony_ci console.log('Caller callWithResult() called'); 709e41f4b71Sopenharmony_ci let retmsg = new MyMessageAble('msg', 'world'); 710e41f4b71Sopenharmony_ci data.readParcelable(retmsg); 711e41f4b71Sopenharmony_ci }) 712e41f4b71Sopenharmony_ci .catch((callErr: BusinessError) => { 713e41f4b71Sopenharmony_ci console.error(`Caller.callWithResult catch error, error.code: ${callErr.code}, error.message: ${callErr.message}`); 714e41f4b71Sopenharmony_ci }); 715e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 716e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 717e41f4b71Sopenharmony_ci }); 718e41f4b71Sopenharmony_ci } 719e41f4b71Sopenharmony_ci} 720e41f4b71Sopenharmony_ci``` 721e41f4b71Sopenharmony_ci 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ci### Caller.release 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_cirelease(): void 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ci主动释放通用组件服务端的通信接口。 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ci**错误码:** 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 734e41f4b71Sopenharmony_ci 735e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 736e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 737e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 738e41f4b71Sopenharmony_ci| 16200002 | The callee does not exist. | 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci**示例:** 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ci```ts 743e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 744e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 745e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_cilet caller: Caller; 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 750e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 751e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 752e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 753e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 754e41f4b71Sopenharmony_ci deviceId: '' 755e41f4b71Sopenharmony_ci }).then((obj) => { 756e41f4b71Sopenharmony_ci caller = obj; 757e41f4b71Sopenharmony_ci try { 758e41f4b71Sopenharmony_ci caller.release(); 759e41f4b71Sopenharmony_ci } catch (releaseErr) { 760e41f4b71Sopenharmony_ci console.error(`Caller.release catch error, error.code: ${releaseErr.code}, error.message: ${releaseErr.message}`); 761e41f4b71Sopenharmony_ci } 762e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 763e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 764e41f4b71Sopenharmony_ci }); 765e41f4b71Sopenharmony_ci } 766e41f4b71Sopenharmony_ci} 767e41f4b71Sopenharmony_ci``` 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ci### Caller.onRelease 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci onRelease(callback: OnReleaseCallback): void 772e41f4b71Sopenharmony_ci 773e41f4b71Sopenharmony_ci注册通用组件服务端Stub(桩)断开监听通知。使用callback异步回调。 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci**参数:** 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 780e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 781e41f4b71Sopenharmony_ci| callback | [OnReleaseCallback](#onreleasecallback) | 是 | 回调函数,返回onRelease回调结果。 | 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ci**错误码:** 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 786e41f4b71Sopenharmony_ci 787e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 788e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 789e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 790e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci**示例:** 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci```ts 795e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 796e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 797e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_cilet caller: Caller; 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 802e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 803e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 804e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 805e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 806e41f4b71Sopenharmony_ci deviceId: '' 807e41f4b71Sopenharmony_ci }).then((obj) => { 808e41f4b71Sopenharmony_ci caller = obj; 809e41f4b71Sopenharmony_ci try { 810e41f4b71Sopenharmony_ci caller.onRelease((str) => { 811e41f4b71Sopenharmony_ci console.log(`Caller OnRelease CallBack is called ${str}`); 812e41f4b71Sopenharmony_ci }); 813e41f4b71Sopenharmony_ci } catch (error) { 814e41f4b71Sopenharmony_ci console.error(`Caller.onRelease catch error, error.code: $error.code}, error.message: ${error.message}`); 815e41f4b71Sopenharmony_ci } 816e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 817e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 818e41f4b71Sopenharmony_ci }); 819e41f4b71Sopenharmony_ci } 820e41f4b71Sopenharmony_ci} 821e41f4b71Sopenharmony_ci``` 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci### Caller.onRemoteStateChange<sup>10+</sup> 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_cionRemoteStateChange(callback: OnRemoteStateChangeCallback): void 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci注册协同场景下跨设备组件状态变化监听通知。使用callback异步回调。 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci**参数:** 832e41f4b71Sopenharmony_ci 833e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 834e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 835e41f4b71Sopenharmony_ci| callback | [OnRemoteStateChangeCallback](#onremotestatechangecallback10) | 是 | 回调函数,返回onRemoteStateChange回调结果。 | 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci**错误码:** 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 842e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 843e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 844e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 845e41f4b71Sopenharmony_ci 846e41f4b71Sopenharmony_ci**示例:** 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci```ts 849e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 850e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 851e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 852e41f4b71Sopenharmony_ci 853e41f4b71Sopenharmony_cilet caller: Caller; 854e41f4b71Sopenharmony_cilet dstDeviceId: string; 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ciexport default class MainAbility extends UIAbility { 857e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 858e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 859e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 860e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 861e41f4b71Sopenharmony_ci deviceId: dstDeviceId 862e41f4b71Sopenharmony_ci }).then((obj) => { 863e41f4b71Sopenharmony_ci caller = obj; 864e41f4b71Sopenharmony_ci try { 865e41f4b71Sopenharmony_ci caller.onRemoteStateChange((str) => { 866e41f4b71Sopenharmony_ci console.log('Remote state changed ' + str); 867e41f4b71Sopenharmony_ci }); 868e41f4b71Sopenharmony_ci } catch (error) { 869e41f4b71Sopenharmony_ci console.error(`Caller.onRemoteStateChange catch error, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}`); 870e41f4b71Sopenharmony_ci } 871e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 872e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${JSON.stringify(err.code)}, error.message: ${JSON.stringify(err.message)}`); 873e41f4b71Sopenharmony_ci }) 874e41f4b71Sopenharmony_ci } 875e41f4b71Sopenharmony_ci} 876e41f4b71Sopenharmony_ci``` 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_ci### Caller.on('release') 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_cion(type: 'release', callback: OnReleaseCallback): void 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci注册通用组件服务端Stub(桩)断开监听通知。使用callback异步回调。 883e41f4b71Sopenharmony_ci 884e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**参数:** 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 889e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 890e41f4b71Sopenharmony_ci| type | string | 是 | 监听releaseCall事件,固定为'release'。 | 891e41f4b71Sopenharmony_ci| callback | [OnReleaseCallback](#onreleasecallback) | 是 | 回调函数,返回on回调结果。 | 892e41f4b71Sopenharmony_ci 893e41f4b71Sopenharmony_ci**错误码:** 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 898e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 899e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 900e41f4b71Sopenharmony_ci| 16200001 | Caller released. The caller has been released. | 901e41f4b71Sopenharmony_ci 902e41f4b71Sopenharmony_ci**示例:** 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci```ts 905e41f4b71Sopenharmony_ciimport { UIAbility, Caller } from '@kit.AbilityKit'; 906e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 907e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_cilet caller: Caller; 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 912e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 913e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 914e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 915e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 916e41f4b71Sopenharmony_ci deviceId: '' 917e41f4b71Sopenharmony_ci }).then((obj) => { 918e41f4b71Sopenharmony_ci caller = obj; 919e41f4b71Sopenharmony_ci try { 920e41f4b71Sopenharmony_ci caller.on('release', (str) => { 921e41f4b71Sopenharmony_ci console.log(`Caller OnRelease CallBack is called ${str}`); 922e41f4b71Sopenharmony_ci }); 923e41f4b71Sopenharmony_ci } catch (error) { 924e41f4b71Sopenharmony_ci console.error(`Caller.on catch error, error.code: ${error.code}, error.message: ${error.message}`); 925e41f4b71Sopenharmony_ci } 926e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 927e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 928e41f4b71Sopenharmony_ci }); 929e41f4b71Sopenharmony_ci } 930e41f4b71Sopenharmony_ci} 931e41f4b71Sopenharmony_ci``` 932e41f4b71Sopenharmony_ci 933e41f4b71Sopenharmony_ci### Caller.off('release') 934e41f4b71Sopenharmony_ci 935e41f4b71Sopenharmony_cioff(type: 'release', callback: OnReleaseCallback): void 936e41f4b71Sopenharmony_ci 937e41f4b71Sopenharmony_ci取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。使用callback异步回调。 938e41f4b71Sopenharmony_ci 939e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 940e41f4b71Sopenharmony_ci 941e41f4b71Sopenharmony_ci**参数:** 942e41f4b71Sopenharmony_ci 943e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 944e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 945e41f4b71Sopenharmony_ci| type | string | 是 | 监听releaseCall事件,固定为'release'。 | 946e41f4b71Sopenharmony_ci| callback | [OnReleaseCallback](#onreleasecallback) | 是 | 回调函数,返回off回调结果。 | 947e41f4b71Sopenharmony_ci 948e41f4b71Sopenharmony_ci**错误码:** 949e41f4b71Sopenharmony_ci 950e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 951e41f4b71Sopenharmony_ci 952e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 953e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 954e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 955e41f4b71Sopenharmony_ci 956e41f4b71Sopenharmony_ci**示例:** 957e41f4b71Sopenharmony_ci 958e41f4b71Sopenharmony_ci```ts 959e41f4b71Sopenharmony_ciimport { UIAbility, Caller, OnReleaseCallback } from '@kit.AbilityKit'; 960e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 961e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_cilet caller: Caller; 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 966e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 967e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 968e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 969e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 970e41f4b71Sopenharmony_ci deviceId: '' 971e41f4b71Sopenharmony_ci }).then((obj) => { 972e41f4b71Sopenharmony_ci caller = obj; 973e41f4b71Sopenharmony_ci try { 974e41f4b71Sopenharmony_ci let onReleaseCallBack: OnReleaseCallback = (str) => { 975e41f4b71Sopenharmony_ci console.log(`Caller OnRelease CallBack is called ${str}`); 976e41f4b71Sopenharmony_ci }; 977e41f4b71Sopenharmony_ci caller.on('release', onReleaseCallBack); 978e41f4b71Sopenharmony_ci caller.off('release', onReleaseCallBack); 979e41f4b71Sopenharmony_ci } catch (error) { 980e41f4b71Sopenharmony_ci console.error(`Caller.on or Caller.off catch error, error.code: ${error.code}, error.message: ${error.message}`); 981e41f4b71Sopenharmony_ci } 982e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 983e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 984e41f4b71Sopenharmony_ci }); 985e41f4b71Sopenharmony_ci } 986e41f4b71Sopenharmony_ci} 987e41f4b71Sopenharmony_ci``` 988e41f4b71Sopenharmony_ci 989e41f4b71Sopenharmony_ci### Caller.off('release') 990e41f4b71Sopenharmony_ci 991e41f4b71Sopenharmony_cioff(type: 'release'): void 992e41f4b71Sopenharmony_ci 993e41f4b71Sopenharmony_ci取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。 994e41f4b71Sopenharmony_ci 995e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 996e41f4b71Sopenharmony_ci 997e41f4b71Sopenharmony_ci**参数:** 998e41f4b71Sopenharmony_ci 999e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1000e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1001e41f4b71Sopenharmony_ci| type | string | 是 | 监听releaseCall事件,固定为'release'。 | 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ci**错误码:** 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 1006e41f4b71Sopenharmony_ci 1007e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1008e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 1009e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci**示例:** 1012e41f4b71Sopenharmony_ci 1013e41f4b71Sopenharmony_ci```ts 1014e41f4b71Sopenharmony_ciimport { UIAbility, Caller, OnReleaseCallback } from '@kit.AbilityKit'; 1015e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 1016e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1017e41f4b71Sopenharmony_ci 1018e41f4b71Sopenharmony_cilet caller: Caller; 1019e41f4b71Sopenharmony_ci 1020e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 1021e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 1022e41f4b71Sopenharmony_ci this.context.startAbilityByCall({ 1023e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 1024e41f4b71Sopenharmony_ci abilityName: 'MainUIAbility', 1025e41f4b71Sopenharmony_ci deviceId: '' 1026e41f4b71Sopenharmony_ci }).then((obj) => { 1027e41f4b71Sopenharmony_ci caller = obj; 1028e41f4b71Sopenharmony_ci try { 1029e41f4b71Sopenharmony_ci let onReleaseCallBack: OnReleaseCallback = (str) => { 1030e41f4b71Sopenharmony_ci console.log(`Caller OnRelease CallBack is called ${str}`); 1031e41f4b71Sopenharmony_ci }; 1032e41f4b71Sopenharmony_ci caller.on('release', onReleaseCallBack); 1033e41f4b71Sopenharmony_ci caller.off('release'); 1034e41f4b71Sopenharmony_ci } catch (error) { 1035e41f4b71Sopenharmony_ci console.error(`Caller.on or Caller.off catch error, error.code: ${error.code}, error.message: ${error.message}`); 1036e41f4b71Sopenharmony_ci } 1037e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 1038e41f4b71Sopenharmony_ci console.error(`Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}`); 1039e41f4b71Sopenharmony_ci }); 1040e41f4b71Sopenharmony_ci } 1041e41f4b71Sopenharmony_ci} 1042e41f4b71Sopenharmony_ci``` 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci## Callee 1045e41f4b71Sopenharmony_ci 1046e41f4b71Sopenharmony_ci通用组件服务端注册和解除客户端caller通知送信的callback接口。 1047e41f4b71Sopenharmony_ci 1048e41f4b71Sopenharmony_ci### Callee.on 1049e41f4b71Sopenharmony_ci 1050e41f4b71Sopenharmony_cion(method: string, callback: CalleeCallback): void 1051e41f4b71Sopenharmony_ci 1052e41f4b71Sopenharmony_ci通用组件服务端注册消息通知callback。 1053e41f4b71Sopenharmony_ci 1054e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci**参数:** 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1059e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1060e41f4b71Sopenharmony_ci| method | string | 是 | 与客户端约定的通知消息字符串。 | 1061e41f4b71Sopenharmony_ci| callback | [CalleeCallback](#calleecallback) | 是 | 一个[rpc.MessageSequence](../apis-ipc-kit/js-apis-rpc.md#messagesequence9)类型入参的js通知同步回调函数, 回调函数至少要返回一个空的[rpc.Parcelable](../apis-ipc-kit/js-apis-rpc.md#parcelable9)数据对象, 其他视为函数执行错误。 | 1062e41f4b71Sopenharmony_ci 1063e41f4b71Sopenharmony_ci**错误码:** 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1066e41f4b71Sopenharmony_ci 1067e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1068e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 1069e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1070e41f4b71Sopenharmony_ci| 16200004 | The method has been registered. | 1071e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1072e41f4b71Sopenharmony_ci 1073e41f4b71Sopenharmony_ci**示例:** 1074e41f4b71Sopenharmony_ci 1075e41f4b71Sopenharmony_ci```ts 1076e41f4b71Sopenharmony_ciimport { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 1077e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 1078e41f4b71Sopenharmony_ci 1079e41f4b71Sopenharmony_ciclass MyMessageAble implements rpc.Parcelable { 1080e41f4b71Sopenharmony_ci name: string 1081e41f4b71Sopenharmony_ci str: string 1082e41f4b71Sopenharmony_ci num: number = 1 1083e41f4b71Sopenharmony_ci constructor(name: string, str: string) { 1084e41f4b71Sopenharmony_ci this.name = name; 1085e41f4b71Sopenharmony_ci this.str = str; 1086e41f4b71Sopenharmony_ci } 1087e41f4b71Sopenharmony_ci marshalling(messageSequence: rpc.MessageSequence) { 1088e41f4b71Sopenharmony_ci messageSequence.writeInt(this.num); 1089e41f4b71Sopenharmony_ci messageSequence.writeString(this.str); 1090e41f4b71Sopenharmony_ci console.log(`MyMessageAble marshalling num[${this.num}] str[${this.str}]`); 1091e41f4b71Sopenharmony_ci return true; 1092e41f4b71Sopenharmony_ci } 1093e41f4b71Sopenharmony_ci unmarshalling(messageSequence: rpc.MessageSequence) { 1094e41f4b71Sopenharmony_ci this.num = messageSequence.readInt(); 1095e41f4b71Sopenharmony_ci this.str = messageSequence.readString(); 1096e41f4b71Sopenharmony_ci console.log(`MyMessageAble unmarshalling num[${this.num}] str[${this.str}]`); 1097e41f4b71Sopenharmony_ci return true; 1098e41f4b71Sopenharmony_ci } 1099e41f4b71Sopenharmony_ci}; 1100e41f4b71Sopenharmony_cilet method = 'call_Function'; 1101e41f4b71Sopenharmony_ci 1102e41f4b71Sopenharmony_cifunction funcCallBack(pdata: rpc.MessageSequence) { 1103e41f4b71Sopenharmony_ci console.log(`Callee funcCallBack is called ${pdata}`); 1104e41f4b71Sopenharmony_ci let msg = new MyMessageAble('test', ''); 1105e41f4b71Sopenharmony_ci pdata.readParcelable(msg); 1106e41f4b71Sopenharmony_ci return new MyMessageAble('test1', 'Callee test'); 1107e41f4b71Sopenharmony_ci} 1108e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 1109e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1110e41f4b71Sopenharmony_ci console.log('Callee onCreate is called'); 1111e41f4b71Sopenharmony_ci try { 1112e41f4b71Sopenharmony_ci this.callee.on(method, funcCallBack); 1113e41f4b71Sopenharmony_ci } catch (error) { 1114e41f4b71Sopenharmony_ci console.error(`Callee.on catch error, error.code: ${error.code}, error.message: ${error.message}`); 1115e41f4b71Sopenharmony_ci } 1116e41f4b71Sopenharmony_ci } 1117e41f4b71Sopenharmony_ci} 1118e41f4b71Sopenharmony_ci``` 1119e41f4b71Sopenharmony_ci 1120e41f4b71Sopenharmony_ci### Callee.off 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_cioff(method: string): void 1123e41f4b71Sopenharmony_ci 1124e41f4b71Sopenharmony_ci解除通用组件服务端注册消息通知callback。 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ci**参数:** 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1131e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1132e41f4b71Sopenharmony_ci| method | string | 是 | 已注册的通知事件字符串。 | 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci**错误码:** 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1137e41f4b71Sopenharmony_ci 1138e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1139e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 1140e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1141e41f4b71Sopenharmony_ci| 16200005 | The method has not been registered. | 1142e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci**示例:** 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ci```ts 1147e41f4b71Sopenharmony_ciimport { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 1148e41f4b71Sopenharmony_ci 1149e41f4b71Sopenharmony_cilet method = 'call_Function'; 1150e41f4b71Sopenharmony_ci 1151e41f4b71Sopenharmony_ciexport default class MainUIAbility extends UIAbility { 1152e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1153e41f4b71Sopenharmony_ci console.log('Callee onCreate is called'); 1154e41f4b71Sopenharmony_ci try { 1155e41f4b71Sopenharmony_ci this.callee.off(method); 1156e41f4b71Sopenharmony_ci } catch (error) { 1157e41f4b71Sopenharmony_ci console.error(`Callee.off catch error, error.code: ${error.code}, error.message: ${error.message}`); 1158e41f4b71Sopenharmony_ci } 1159e41f4b71Sopenharmony_ci } 1160e41f4b71Sopenharmony_ci} 1161e41f4b71Sopenharmony_ci``` 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci## OnReleaseCallback 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_ci### (msg: string) 1166e41f4b71Sopenharmony_ci 1167e41f4b71Sopenharmony_ci(msg: string): void 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci注册通用组件服务端Stub(桩)断开监听通知的回调函数类型。 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ci**参数:** 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1176e41f4b71Sopenharmony_ci| --- | ----- | --- | -------- | 1177e41f4b71Sopenharmony_ci| msg | string | 是 | 用于传递释放消息。 | 1178e41f4b71Sopenharmony_ci 1179e41f4b71Sopenharmony_ci## OnRemoteStateChangeCallback<sup>10+</sup> 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci### (msg: string) 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ci(msg: string): void 1184e41f4b71Sopenharmony_ci 1185e41f4b71Sopenharmony_ci注册协同场景下跨设备组件状态变化监听通知的回调函数类型。 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 1188e41f4b71Sopenharmony_ci 1189e41f4b71Sopenharmony_ci**参数:** 1190e41f4b71Sopenharmony_ci 1191e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1192e41f4b71Sopenharmony_ci| --- | ----- | --- | -------- | 1193e41f4b71Sopenharmony_ci| msg | string | 是 | 用于传递释放消息。 | 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci## CalleeCallback 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ci### (indata: rpc.MessageSequence) 1198e41f4b71Sopenharmony_ci 1199e41f4b71Sopenharmony_ci(indata: rpc.MessageSequence): rpc.Parcelable 1200e41f4b71Sopenharmony_ci 1201e41f4b71Sopenharmony_ci通用组件服务端注册消息通知的回调函数类型。 1202e41f4b71Sopenharmony_ci 1203e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci**参数:** 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1208e41f4b71Sopenharmony_ci| --- | ----- | --- | -------- | 1209e41f4b71Sopenharmony_ci| indata | [rpc.MessageSequence](../apis-ipc-kit/js-apis-rpc.md#messagesequence9) | 是 | 发送需传递的数据。 | 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci**返回值:** 1212e41f4b71Sopenharmony_ci 1213e41f4b71Sopenharmony_ci| 类型 | 说明 | 1214e41f4b71Sopenharmony_ci| ------------ | ------------------------------------- | 1215e41f4b71Sopenharmony_ci| [rpc.Parcelable](../apis-ipc-kit/js-apis-rpc.md#parcelable9) | 返回的数据对象。 | 1216