1e41f4b71Sopenharmony_ci# @ohos.app.ability.sendableContextManager (sendableContextManager) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_cisendableContextManager模块提供Context与[SendableContext](js-apis-inner-application-sendableContext.md)相互转换的能力。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 12 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> - 本模块接口仅可在Stage模型下使用。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 使用场景 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci本模块主要用于ArkTS并发实例间(包括主线程、TaskPool&Worker工作线程)的数据传递。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci例如,以主线程向子线程传递Sendable数据,转换过程如下: 15e41f4b71Sopenharmony_ci- 主线程向子线程传递Sendable数据时,需要将Context转换为SendableContext。 16e41f4b71Sopenharmony_ci- 子线程使用Sendable数据时,需要将SendableContext转换为Context。 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci这里的Context与[createModuleContext](js-apis-inner-application-context.md#contextcreatemodulecontext)方法创建的Context不同,具体差异如下: 19e41f4b71Sopenharmony_ci- 与SendableContext相互转换的Context:ArkTS并发实例持有的应用侧Context是不同的实例,底层对应同一个Context对象。当一个实例中Context属性和方法被修改时,相关实例中的Context属性和方法将会同步修改。其中,Context实例中的eventHub属性比较特殊,不同实例中的eventHub是独立的对象,不支持跨ArkTS实例使用。 20e41f4b71Sopenharmony_ci- 通过[createModuleContext](js-apis-inner-application-context.md#contextcreatemodulecontext)创建的Context:ArkTS并发实例持有的应用侧Context是不同的实例,底层对应不同的Context对象。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## 约束限制 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci“Context转换为SendableContext”和“SendableContext转换为Context”两个环节中的Context类型必须保持一致。目前支持转换的Context包括[Context](js-apis-inner-application-context.md)、[ApplicationContext](js-apis-inner-application-applicationContext.md)、[AbilityStageContext](js-apis-inner-application-abilityStageContext.md)、[UIAbilityContext](js-apis-inner-application-uiAbilityContext.md)。 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## 导入模块 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci```ts 29e41f4b71Sopenharmony_ciimport { sendableContextManager } from '@kit.AbilityKit'; 30e41f4b71Sopenharmony_ci``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## 属性 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 39e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 40e41f4b71Sopenharmony_ci| SendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext二级模块。 | 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**示例:** 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci```ts 45e41f4b71Sopenharmony_ciimport { sendableContextManager } from '@kit.AbilityKit'; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_cilet sendableContext: sendableContextManager.SendableContext; 48e41f4b71Sopenharmony_ci``` 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci## sendableContextManager.convertFromContext 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciconvertFromContext(context: common.Context): SendableContext; 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci将Context转换为SendableContext对象。 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**参数:** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 63e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 64e41f4b71Sopenharmony_ci| context | common.Context | 是 | Context对象,支持Context基类,ApplicationContext、AbilityStageContext和UIAbilityContext子类。 | 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**错误码**: 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 71e41f4b71Sopenharmony_ci| ------- | ------- | 72e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**示例:** 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci```ts 77e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, sendableContextManager } from '@kit.AbilityKit'; 78e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 79e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci@Sendable 82e41f4b71Sopenharmony_ciexport class SendableObject { 83e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext) { 84e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 88e41f4b71Sopenharmony_ci // other sendable object 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 92e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 95e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci // convert and post 98e41f4b71Sopenharmony_ci try { 99e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 100e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext); 101e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 102e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 103e41f4b71Sopenharmony_ci } catch (error) { 104e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci} 108e41f4b71Sopenharmony_ci``` 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci## sendableContextManager.convertToContext 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ciconvertToContext(sendableContext: SendableContext): common.Context 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci将SendableContext对象转换为Context。 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**参数:** 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 123e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 124e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**错误码**: 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 131e41f4b71Sopenharmony_ci| ------- | ------- | 132e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**示例:** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci主线程传递Context: 137e41f4b71Sopenharmony_ci```ts 138e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 139e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 140e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci@Sendable 143e41f4b71Sopenharmony_ciexport class SendableObject { 144e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 145e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 146e41f4b71Sopenharmony_ci this.contextName = contextName; 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 150e41f4b71Sopenharmony_ci contextName: string; 151e41f4b71Sopenharmony_ci} 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 154e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 157e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci // convert and post 160e41f4b71Sopenharmony_ci try { 161e41f4b71Sopenharmony_ci let context: common.Context = this.context as common.Context; 162e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(context); 163e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'BaseContext'); 164e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 165e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 166e41f4b71Sopenharmony_ci } catch (error) { 167e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci } 170e41f4b71Sopenharmony_ci} 171e41f4b71Sopenharmony_ci``` 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciWorker线程接收Context: 174e41f4b71Sopenharmony_ci```ts 175e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 176e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 177e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci@Sendable 180e41f4b71Sopenharmony_ciexport class SendableObject { 181e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 182e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 183e41f4b71Sopenharmony_ci this.contextName = contextName; 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 187e41f4b71Sopenharmony_ci contextName: string; 188e41f4b71Sopenharmony_ci} 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 193e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 194e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 195e41f4b71Sopenharmony_ci if (object.contextName == 'BaseContext') { 196e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to context.'); 197e41f4b71Sopenharmony_ci try { 198e41f4b71Sopenharmony_ci let context: common.Context = sendableContextManager.convertToContext(sendableContext); 199e41f4b71Sopenharmony_ci // 获取context后获取沙箱路径 200e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 201e41f4b71Sopenharmony_ci } catch (error) { 202e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error)); 203e41f4b71Sopenharmony_ci } 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci} 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 208e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 209e41f4b71Sopenharmony_ci} 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 212e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 213e41f4b71Sopenharmony_ci} 214e41f4b71Sopenharmony_ci``` 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci## sendableContextManager.convertToApplicationContext 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ciconvertToApplicationContext(sendableContext: SendableContext): common.ApplicationContext 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci将SendableContext对象转换为ApplicationContext。 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci**参数:** 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 229e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 230e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**错误码**: 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 237e41f4b71Sopenharmony_ci| ------- | ------- | 238e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci**示例:** 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci主线程传递Context: 243e41f4b71Sopenharmony_ci```ts 244e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 245e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 246e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci@Sendable 249e41f4b71Sopenharmony_ciexport class SendableObject { 250e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 251e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 252e41f4b71Sopenharmony_ci this.contextName = contextName; 253e41f4b71Sopenharmony_ci } 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 256e41f4b71Sopenharmony_ci contextName: string; 257e41f4b71Sopenharmony_ci} 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 260e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 263e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci // convert and post 266e41f4b71Sopenharmony_ci try { 267e41f4b71Sopenharmony_ci let context: common.Context = this.context as common.Context; 268e41f4b71Sopenharmony_ci let applicationContext = context.getApplicationContext(); 269e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(applicationContext); 270e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'ApplicationContext'); 271e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 272e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 273e41f4b71Sopenharmony_ci } catch (error) { 274e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci} 278e41f4b71Sopenharmony_ci``` 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciWorker线程接收Context: 281e41f4b71Sopenharmony_ci```ts 282e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 283e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 284e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci@Sendable 287e41f4b71Sopenharmony_ciexport class SendableObject { 288e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 289e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 290e41f4b71Sopenharmony_ci this.contextName = contextName; 291e41f4b71Sopenharmony_ci } 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 294e41f4b71Sopenharmony_ci contextName: string; 295e41f4b71Sopenharmony_ci} 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 300e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 301e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 302e41f4b71Sopenharmony_ci if (object.contextName == 'ApplicationContext') { 303e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to application context.'); 304e41f4b71Sopenharmony_ci try { 305e41f4b71Sopenharmony_ci let context: common.ApplicationContext = sendableContextManager.convertToApplicationContext(sendableContext); 306e41f4b71Sopenharmony_ci // 获取context后获取沙箱路径 307e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 308e41f4b71Sopenharmony_ci } catch (error) { 309e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToApplicationContext failed %{public}s', JSON.stringify(error)); 310e41f4b71Sopenharmony_ci } 311e41f4b71Sopenharmony_ci } 312e41f4b71Sopenharmony_ci} 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 315e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 316e41f4b71Sopenharmony_ci} 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 319e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 320e41f4b71Sopenharmony_ci} 321e41f4b71Sopenharmony_ci``` 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci## sendableContextManager.convertToAbilityStageContext 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ciconvertToAbilityStageContext(sendableContext: SendableContext): common.AbilityStageContext 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci将SendableContext对象转换为AbilityStageContext。 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**参数:** 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 336e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 337e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci**错误码**: 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 344e41f4b71Sopenharmony_ci| ------- | ------- | 345e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**示例:** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci主线程传递Context: 350e41f4b71Sopenharmony_ci```ts 351e41f4b71Sopenharmony_ciimport { UIAbility, sendableContextManager } from '@kit.AbilityKit'; 352e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 353e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci@Sendable 356e41f4b71Sopenharmony_ciexport class SendableObject { 357e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 358e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 359e41f4b71Sopenharmony_ci this.contextName = contextName; 360e41f4b71Sopenharmony_ci } 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 363e41f4b71Sopenharmony_ci contextName: string; 364e41f4b71Sopenharmony_ci} 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 367e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci onCreate(): void { 370e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate'); 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci // convert and post 373e41f4b71Sopenharmony_ci try { 374e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 375e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'AbilityStageContext'); 376e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage post message'); 377e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 378e41f4b71Sopenharmony_ci } catch (error) { 379e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci} 383e41f4b71Sopenharmony_ci``` 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ciWorker线程接收Context: 386e41f4b71Sopenharmony_ci```ts 387e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 388e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 389e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci@Sendable 392e41f4b71Sopenharmony_ciexport class SendableObject { 393e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 394e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 395e41f4b71Sopenharmony_ci this.contextName = contextName; 396e41f4b71Sopenharmony_ci } 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 399e41f4b71Sopenharmony_ci contextName: string; 400e41f4b71Sopenharmony_ci} 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 405e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 406e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 407e41f4b71Sopenharmony_ci if (object.contextName == 'AbilityStageContext') { 408e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to abilitystage context.'); 409e41f4b71Sopenharmony_ci try { 410e41f4b71Sopenharmony_ci let context: common.AbilityStageContext = sendableContextManager.convertToAbilityStageContext(sendableContext); 411e41f4b71Sopenharmony_ci // 获取context后获取沙箱路径 412e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 413e41f4b71Sopenharmony_ci } catch (error) { 414e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToAbilityStageContext failed %{public}s', JSON.stringify(error)); 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci } 417e41f4b71Sopenharmony_ci} 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 420e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 421e41f4b71Sopenharmony_ci} 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 424e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 425e41f4b71Sopenharmony_ci} 426e41f4b71Sopenharmony_ci``` 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci## sendableContextManager.convertToUIAbilityContext 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ciconvertToUIAbilityContext(sendableContext: SendableContext): common.UIAbilityContext 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci将SendableContext对象转换为UIAbilityContext。 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**参数:** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 441e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 442e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**错误码**: 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 449e41f4b71Sopenharmony_ci| ------- | ------- | 450e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci**示例:** 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci主线程传递Context: 455e41f4b71Sopenharmony_ci```ts 456e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 457e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 458e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci@Sendable 461e41f4b71Sopenharmony_ciexport class SendableObject { 462e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 463e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 464e41f4b71Sopenharmony_ci this.contextName = contextName; 465e41f4b71Sopenharmony_ci } 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 468e41f4b71Sopenharmony_ci contextName: string; 469e41f4b71Sopenharmony_ci} 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 472e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 475e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci // convert and post 478e41f4b71Sopenharmony_ci try { 479e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 480e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'EntryAbilityContext'); 481e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 482e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 483e41f4b71Sopenharmony_ci } catch (error) { 484e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 485e41f4b71Sopenharmony_ci } 486e41f4b71Sopenharmony_ci } 487e41f4b71Sopenharmony_ci} 488e41f4b71Sopenharmony_ci``` 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ciWorker线程接收Context: 491e41f4b71Sopenharmony_ci```ts 492e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 493e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 494e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci@Sendable 497e41f4b71Sopenharmony_ciexport class SendableObject { 498e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 499e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 500e41f4b71Sopenharmony_ci this.contextName = contextName; 501e41f4b71Sopenharmony_ci } 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 504e41f4b71Sopenharmony_ci contextName: string; 505e41f4b71Sopenharmony_ci} 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 510e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 511e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 512e41f4b71Sopenharmony_ci if (object.contextName == 'EntryAbilityContext') { 513e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to uiability context.'); 514e41f4b71Sopenharmony_ci try { 515e41f4b71Sopenharmony_ci let context: common.UIAbilityContext = sendableContextManager.convertToUIAbilityContext(sendableContext); 516e41f4b71Sopenharmony_ci // 获取context后获取沙箱路径 517e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 518e41f4b71Sopenharmony_ci } catch (error) { 519e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToUIAbilityContext failed %{public}s', JSON.stringify(error)); 520e41f4b71Sopenharmony_ci } 521e41f4b71Sopenharmony_ci } 522e41f4b71Sopenharmony_ci} 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 525e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 526e41f4b71Sopenharmony_ci} 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 529e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 530e41f4b71Sopenharmony_ci} 531e41f4b71Sopenharmony_ci``` 532