1# @ohos.app.appstartup.StartupTask (启动框架任务) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @yzkp--> 5<!--Designer: @yzkp--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9本模块提供启动任务的相关能力。 10 11> **说明:** 12> 13> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> 本模块接口仅可在Stage模型下使用。 16 17## 导入模块 18 19```js 20import { StartupTask } from '@kit.AbilityKit'; 21``` 22 23## StartupTask 24 25该类提供启动任务的相关能力,使用[@Sendable装饰器](../../arkts-utils/arkts-sendable.md#sendable装饰器)装饰。 26 27**装饰器类型**:\@Sendable 28 29### onDependencyCompleted 30 31onDependencyCompleted?(dependency: string, result: Object): void 32 33当依赖的启动任务执行完成时该方法将会被调用。 34 35**系统能力**:SystemCapability.Ability.AppStartup 36 37**参数:** 38 39| 参数名 | 类型 | 必填 | 说明 | 40| -------- | -------- | -------- | -------- | 41| dependency | string | 是 | 依赖的启动任务名称。 | 42| result | Object | 是 | 依赖启动任务执行的结果。 | 43 44**示例:** 45 46```ts 47import { StartupTask, common } from '@kit.AbilityKit'; 48import { hilog } from '@kit.PerformanceAnalysisKit'; 49 50@Sendable 51export default class StartupTask_001 extends StartupTask { 52 constructor() { 53 super(); 54 } 55 56 async init(context: common.AbilityStageContext) { 57 // ... 58 } 59 60 onDependencyCompleted(dependence: string, result: Object): void { 61 hilog.info(0x0000, 'testTag', 'StartupTask_001 onDependencyCompleted, dependence: %{public}s, result: %{public}s', 62 dependence, JSON.stringify(result)); 63 // ... 64 } 65} 66``` 67 68 69### init 70 71init(context: AbilityStageContext): Promise\<Object \| void\> 72 73启动任务执行的初始化业务。 74 75**系统能力**:SystemCapability.Ability.AppStartup 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| -------- | -------- | -------- | -------- | 81| context | [AbilityStageContext](js-apis-inner-application-abilityStageContext.md) | 是 | AbilityStage的上下文环境 | 82 83**返回值:** 84 85| 类型 | 说明 | 86| -------- | -------- | 87| Promise\<Object \| void\> | Promise对象,返回启动任务执行结果对象。 | 88 89**示例:** 90 91```ts 92import { StartupTask, common } from '@kit.AbilityKit'; 93import { hilog } from '@kit.PerformanceAnalysisKit'; 94 95@Sendable 96export default class StartupTask_001 extends StartupTask { 97 constructor() { 98 super(); 99 } 100 async init(context: common.AbilityStageContext) { 101 hilog.info(0x0000, 'testTag', 'StartupTask_001 init.'); 102 // ... 103 104 return "StartupTask_001"; 105 } 106 107 onDependencyCompleted(dependence: string, result: Object): void { 108 // ... 109 } 110} 111``` 112