1e41f4b71Sopenharmony_ci# ApplicationContext(系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciApplicationContext模块继承自[Context](js-apis-inner-application-context.md),提供开发者应用级别的的上下文的能力,包括提供注册及取消注册应用内组件生命周期的监听接口。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 当前页面仅包含本模块的系统接口,其他公开接口参见[ApplicationContext](js-apis-inner-application-applicationContext.md)。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## 使用说明 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci在使用ApplicationContext的功能前,需要通过context的实例获取。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## ApplicationContext.preloadUIExtensionAbility<sup>12+</sup> 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_cipreloadUIExtensionAbility(want: Want): Promise\<void\> 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci预加载指定UIExtensionAbility实例。 25e41f4b71Sopenharmony_ci被预加载的UIExtensionAbility实例会执行到UIExtensionAbility的OnCreate生命周期,然后等待被当前应用正式加载。支持多次预加载UIExtensionAbility实例,每次正式加载时,会使一个预加载的UIExtensionAbility实例从OnCreate继续完成UIExtensionAbility的生命周期。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口。 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 32e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 33e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 预加载UIExtensionAbility的want信息。 | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**返回值:** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| 类型 | 说明 | 38e41f4b71Sopenharmony_ci| -------- | -------- | 39e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**错误码:** 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 46e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 47e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 48e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 49e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 50e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 51e41f4b71Sopenharmony_ci| 16200011 | The context does not exist. | 52e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**示例:** 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci```ts 57e41f4b71Sopenharmony_ciimport { UIAbility, Want } from '@kit.AbilityKit'; 58e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 61e41f4b71Sopenharmony_ci onCreate() { 62e41f4b71Sopenharmony_ci let want: Want = { 63e41f4b71Sopenharmony_ci bundleName: 'com.ohos.uiextensionprovider', 64e41f4b71Sopenharmony_ci abilityName: 'UIExtensionProvider', 65e41f4b71Sopenharmony_ci moduleName: 'entry', 66e41f4b71Sopenharmony_ci parameters: { 67e41f4b71Sopenharmony_ci //与UIExtensionAbility在module.json5中"type"字段配置一致 68e41f4b71Sopenharmony_ci 'ability.want.params.uiExtensionType': 'sys/commonUI' 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci }; 71e41f4b71Sopenharmony_ci try { 72e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 73e41f4b71Sopenharmony_ci applicationContext.preloadUIExtensionAbility(want) 74e41f4b71Sopenharmony_ci .then(() => { 75e41f4b71Sopenharmony_ci // 执行正常业务 76e41f4b71Sopenharmony_ci console.info('preloadUIExtensionAbility succeed'); 77e41f4b71Sopenharmony_ci }) 78e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 79e41f4b71Sopenharmony_ci // 处理业务逻辑错误 80e41f4b71Sopenharmony_ci console.error('preloadUIExtensionAbility failed'); 81e41f4b71Sopenharmony_ci }); 82e41f4b71Sopenharmony_ci } catch (err) { 83e41f4b71Sopenharmony_ci // 处理入参错误异常 84e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 85e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 86e41f4b71Sopenharmony_ci console.error(`preloadUIExtensionAbility failed. code: ${code}, msg: ${message}`); 87e41f4b71Sopenharmony_ci } 88e41f4b71Sopenharmony_ci } 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci``` 91