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