# @ohos.app.ability.sendableContextManager (sendableContextManager) sendableContextManager模å—æä¾›Context与[SendableContext](js-apis-inner-application-sendableContext.md)相互转æ¢çš„能力。 > **说明:** > > - 本模å—首批接å£ä»ŽAPI version 12 开始支æŒã€‚åŽç»ç‰ˆæœ¬çš„新增接å£ï¼Œé‡‡ç”¨ä¸Šè§’æ ‡å•ç‹¬æ ‡è®°æŽ¥å£çš„起始版本。 > - 本模å—接å£ä»…å¯åœ¨Stage模型下使用。 ## 使用场景 本模å—主è¦ç”¨äºŽArkTS并å‘实例间(包括主线程ã€TaskPool&Worker工作线程)的数æ®ä¼ 递。 例如,以主线程å‘åçº¿ç¨‹ä¼ é€’Sendableæ•°æ®ï¼Œè½¬æ¢è¿‡ç¨‹å¦‚下: - 主线程å‘åçº¿ç¨‹ä¼ é€’Sendableæ•°æ®æ—¶ï¼Œéœ€è¦å°†Context转æ¢ä¸ºSendableContext。 - å线程使用Sendableæ•°æ®æ—¶ï¼Œéœ€è¦å°†SendableContext转æ¢ä¸ºContext。 这里的Context与[createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)方法创建的Contextä¸åŒï¼Œå…·ä½“差异如下: - 与SendableContext相互转æ¢çš„Context:ArkTS并å‘实例æŒæœ‰çš„应用侧Context是ä¸åŒçš„实例,底层对应åŒä¸€ä¸ªContext对象。当一个实例ä¸Context属性和方法被修改时,相关实例ä¸çš„Context属性和方法将会åŒæ¥ä¿®æ”¹ã€‚å…¶ä¸ï¼ŒContext实例ä¸çš„eventHub属性比较特殊,ä¸åŒå®žä¾‹ä¸çš„eventHub是独立的对象,ä¸æ”¯æŒè·¨ArkTS实例使用。 - 通过[createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)创建的Context:ArkTS并å‘实例æŒæœ‰çš„应用侧Context是ä¸åŒçš„实例,底层对应ä¸åŒçš„Context对象。 ## 约æŸé™åˆ¶ “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)。 ## å¯¼å…¥æ¨¡å— ```ts import { sendableContextManager } from '@kit.AbilityKit'; ``` ## 属性 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ | å称 | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | SendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext二级模å—。 | **示例:** ```ts import { sendableContextManager } from '@kit.AbilityKit'; let sendableContext: sendableContextManager.SendableContext; ``` ## sendableContextManager.convertFromContext convertFromContext(context: common.Context): SendableContext; å°†Context转æ¢ä¸ºSendableContext对象。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ **å‚数:** | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | context | common.Context | 是 | Context对象,支æŒContext基类,ApplicationContextã€AbilityStageContextå’ŒUIAbilityContextå类。 | **错误ç **: 以下错误ç 详细介ç»è¯·å‚考[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ------- | ------- | | 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | **示例:** ```ts import { AbilityConstant, UIAbility, Want, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext) { this.sendableContext = sendableContext; } sendableContext: sendableContextManager.SendableContext; // other sendable object } export default class EntryAbility extends UIAbility { worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); // convert and post try { let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); let object: SendableObject = new SendableObject(sendableContext); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); this.worker.postMessageWithSharedSendable(object); } catch (error) { hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); } } } ``` ## sendableContextManager.convertToContext convertToContext(sendableContext: SendableContext): common.Context å°†SendableContext对象转æ¢ä¸ºContext。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ **å‚数:** | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | **错误ç **: 以下错误ç 详细介ç»è¯·å‚考[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ------- | ------- | | 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | **示例:** ä¸»çº¿ç¨‹ä¼ é€’Context: ```ts import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } export default class EntryAbility extends UIAbility { worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); // convert and post try { let context: common.Context = this.context as common.Context; let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(context); let object: SendableObject = new SendableObject(sendableContext, 'BaseContext'); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); this.worker.postMessageWithSharedSendable(object); } catch (error) { hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); } } } ``` Worker线程接收Context: ```ts import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; import { common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } const workerPort: ThreadWorkerGlobalScope = worker.workerPort; workerPort.onmessage = (e: MessageEvents) => { let object: SendableObject = e.data; let sendableContext: sendableContextManager.SendableContext = object.sendableContext; if (object.contextName == 'BaseContext') { hilog.info(0x0000, 'testTag', '%{public}s', 'convert to context.'); try { let context: common.Context = sendableContextManager.convertToContext(sendableContext); // 获å–contextåŽèŽ·å–沙箱路径 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); } catch (error) { hilog.error(0x0000, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error)); } } } workerPort.onmessageerror = (e: MessageEvents) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); } workerPort.onerror = (e: ErrorEvent) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); } ``` ## sendableContextManager.convertToApplicationContext convertToApplicationContext(sendableContext: SendableContext): common.ApplicationContext å°†SendableContext对象转æ¢ä¸ºApplicationContext。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ **å‚数:** | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | **错误ç **: 以下错误ç 详细介ç»è¯·å‚考[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ------- | ------- | | 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | **示例:** ä¸»çº¿ç¨‹ä¼ é€’Context: ```ts import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } export default class EntryAbility extends UIAbility { worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); // convert and post try { let context: common.Context = this.context as common.Context; let applicationContext = context.getApplicationContext(); let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(applicationContext); let object: SendableObject = new SendableObject(sendableContext, 'ApplicationContext'); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); this.worker.postMessageWithSharedSendable(object); } catch (error) { hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); } } } ``` Worker线程接收Context: ```ts import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; import { common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } const workerPort: ThreadWorkerGlobalScope = worker.workerPort; workerPort.onmessage = (e: MessageEvents) => { let object: SendableObject = e.data; let sendableContext: sendableContextManager.SendableContext = object.sendableContext; if (object.contextName == 'ApplicationContext') { hilog.info(0x0000, 'testTag', '%{public}s', 'convert to application context.'); try { let context: common.ApplicationContext = sendableContextManager.convertToApplicationContext(sendableContext); // 获å–contextåŽèŽ·å–沙箱路径 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); } catch (error) { hilog.error(0x0000, 'testTag', 'convertToApplicationContext failed %{public}s', JSON.stringify(error)); } } } workerPort.onmessageerror = (e: MessageEvents) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); } workerPort.onerror = (e: ErrorEvent) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); } ``` ## sendableContextManager.convertToAbilityStageContext convertToAbilityStageContext(sendableContext: SendableContext): common.AbilityStageContext å°†SendableContext对象转æ¢ä¸ºAbilityStageContext。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ **å‚数:** | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | **错误ç **: 以下错误ç 详细介ç»è¯·å‚考[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ------- | ------- | | 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | **示例:** ä¸»çº¿ç¨‹ä¼ é€’Context: ```ts import { UIAbility, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } export default class EntryAbility extends UIAbility { worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); onCreate(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate'); // convert and post try { let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); let object: SendableObject = new SendableObject(sendableContext, 'AbilityStageContext'); hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage post message'); this.worker.postMessageWithSharedSendable(object); } catch (error) { hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); } } } ``` Worker线程接收Context: ```ts import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; import { common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } const workerPort: ThreadWorkerGlobalScope = worker.workerPort; workerPort.onmessage = (e: MessageEvents) => { let object: SendableObject = e.data; let sendableContext: sendableContextManager.SendableContext = object.sendableContext; if (object.contextName == 'AbilityStageContext') { hilog.info(0x0000, 'testTag', '%{public}s', 'convert to abilitystage context.'); try { let context: common.AbilityStageContext = sendableContextManager.convertToAbilityStageContext(sendableContext); // 获å–contextåŽèŽ·å–沙箱路径 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); } catch (error) { hilog.error(0x0000, 'testTag', 'convertToAbilityStageContext failed %{public}s', JSON.stringify(error)); } } } workerPort.onmessageerror = (e: MessageEvents) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); } workerPort.onerror = (e: ErrorEvent) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); } ``` ## sendableContextManager.convertToUIAbilityContext convertToUIAbilityContext(sendableContext: SendableContext): common.UIAbilityContext å°†SendableContext对象转æ¢ä¸ºUIAbilityContext。 **系统能力**:SystemCapability.Ability.AbilityRuntime.Core **原å化æœåŠ¡API**:从API version 12开始,该接å£æ”¯æŒåœ¨åŽŸå化æœåŠ¡ä¸ä½¿ç”¨ã€‚ **å‚数:** | å‚æ•°å | 类型 | å¿…å¡« | 说明 | | ------- | ------- | ------- | ------- | | sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | 是 | SendableContext对象。 | **错误ç **: 以下错误ç 详细介ç»è¯·å‚考[通用错误ç ](../errorcode-universal.md)。 | 错误ç ID | é”™è¯¯ä¿¡æ¯ | | ------- | ------- | | 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | **示例:** ä¸»çº¿ç¨‹ä¼ é€’Context: ```ts import { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { worker } from '@kit.ArkTS'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } export default class EntryAbility extends UIAbility { worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); // convert and post try { let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); let object: SendableObject = new SendableObject(sendableContext, 'EntryAbilityContext'); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); this.worker.postMessageWithSharedSendable(object); } catch (error) { hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); } } } ``` Worker线程接收Context: ```ts import { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; import { common, sendableContextManager } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; @Sendable export class SendableObject { constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { this.sendableContext = sendableContext; this.contextName = contextName; } sendableContext: sendableContextManager.SendableContext; contextName: string; } const workerPort: ThreadWorkerGlobalScope = worker.workerPort; workerPort.onmessage = (e: MessageEvents) => { let object: SendableObject = e.data; let sendableContext: sendableContextManager.SendableContext = object.sendableContext; if (object.contextName == 'EntryAbilityContext') { hilog.info(0x0000, 'testTag', '%{public}s', 'convert to uiability context.'); try { let context: common.UIAbilityContext = sendableContextManager.convertToUIAbilityContext(sendableContext); // 获å–contextåŽèŽ·å–沙箱路径 hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); } catch (error) { hilog.error(0x0000, 'testTag', 'convertToUIAbilityContext failed %{public}s', JSON.stringify(error)); } } } workerPort.onmessageerror = (e: MessageEvents) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); } workerPort.onerror = (e: ErrorEvent) => { hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); } ```