• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.appstartup.startupManager
2
3The startupManager module provides APIs to manage startup tasks in AppStartup. It can be called only in the main thread.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> This module supports .so file preloading since API version 18.
10>
11> The APIs of this module can be used only in the stage model.
12
13## Modules to Import
14
15```ts
16import { startupManager }  from '@kit.AbilityKit';
17```
18
19## startupManager.run
20run(startupTasks: Array\<string\>, config?: StartupConfig): Promise\<void\>
21
22Runs startup tasks or loads .so files.
23
24**System capability**: SystemCapability.Ability.AppStartup
25
26**Parameters**
27
28  | Name| Type| Mandatory| Description|
29  | -------- | -------- | -------- | -------- |
30  | startupTasks | Array\<string\> | Yes| Array of class names of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task and names of .so files to be preloaded.|
31  | config | [StartupConfig](./js-apis-app-appstartup-startupConfig.md) | No| Configuration for the AppStartup timeout and startup task listener.|
32
33**Return value**
34
35| Type| Description|
36| -------- | -------- |
37| Promise\<void\> | Promise that returns no value.|
38
39**Error codes**
40
41For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
42
43  | ID| Error Message|
44  | ------- | -------------------------------- |
45  | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
46  | 16000050 | Internal error. |
47  | 28800001 | Startup task or its dependency not found. |
48  | 28800002  | The startup tasks have circular dependencies. |
49  | 28800003 | An error occurred while running the startup tasks. |
50  | 28800004 | Running startup tasks timeout. |
51
52**Example**
53
54```ts
55import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit';
56import { hilog } from '@kit.PerformanceAnalysisKit';
57import { BusinessError } from '@kit.BasicServicesKit';
58
59export default class EntryAbility extends UIAbility {
60  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
61    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
62    let startParams = ["StartupTask_001", "libentry_001"];
63    try {
64      // Manually call the run method.
65      startupManager.run(startParams).then(() => {
66        console.log('StartupTest startupManager run then, startParams = ');
67      }).catch((error: BusinessError) => {
68        console.info("StartupTest promise catch error, error = " + JSON.stringify(error));
69        console.info("StartupTest promise catch error, startParams = "
70          + JSON.stringify(startParams));
71      })
72    } catch (error) {
73      let errMsg = JSON.stringify(error);
74      let errCode: number = error.code;
75      console.log('Startup catch error , errCode= ' + errCode);
76      console.log('Startup catch error ,error= ' + errMsg);
77    }
78  }
79  // ...
80}
81```
82
83## startupManager.removeAllStartupTaskResults
84
85removeAllStartupTaskResults(): void
86
87Removes all startup task results.
88
89If there are preloading tasks for .so files, the corresponding .so files is set to the unloaded state. However, .so files that have already been loaded in the cache will not be removed.
90
91**System capability**: SystemCapability.Ability.AppStartup
92
93**Example**
94
95```ts
96import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit';
97import { window } from '@kit.ArkUI';
98import { hilog } from '@kit.PerformanceAnalysisKit';
99
100export default class EntryAbility extends UIAbility {
101  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
102    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
103    startupManager.run(["StartupTask_001", "libentry_001"]).then(() => {
104      console.info("StartupTask_001 init successful");
105    })
106  }
107
108  onWindowStageCreate(windowStage: window.WindowStage) {
109    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
110    startupManager.removeAllStartupTaskResults(); // Remove all startup task results.
111
112    windowStage.loadContent('pages/Index', (err, data) => {
113      if (err.code) {
114        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
115        return;
116      }
117      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
118    });
119  }
120}
121```
122
123
124## startupManager.getStartupTaskResult
125
126getStartupTaskResult(startupTask: string): Object
127
128Obtains the execution result of a startup task or .so file preloading task.
129
130**System capability**: SystemCapability.Ability.AppStartup
131
132**Parameters**
133
134  | Name| Type| Mandatory| Description|
135  | -------- | -------- | -------- | -------- |
136  | startupTask | string | Yes| Class name of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task or .so file name. All the startup tasks must implement the [StartupTask](js-apis-app-appstartup-startupTask.md) API.|
137
138**Return value**
139
140  | Type| Description|
141  | -------- | -------- |
142  | Object | Execution result of the startup task if a startup task name is passed.<br> undefined if a .so file name is passed.|
143
144**Error codes**
145
146For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
147
148  | ID| Error Message|
149  | ------- | -------------------------------- |
150  | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
151
152**Example**
153
154```ts
155import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit';
156import { window } from '@kit.ArkUI';
157import { hilog } from '@kit.PerformanceAnalysisKit';
158
159export default class EntryAbility extends UIAbility {
160  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
161    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
162    startupManager.run(["StartupTask_001"]).then(() => {
163      console.info("StartupTask_001 init successful");
164    })
165  }
166
167  onWindowStageCreate(windowStage: window.WindowStage) {
168    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
169    let result = startupManager.getStartupTaskResult("StartupTask_001"); // Manually obtain the startup task result.
170    console.info("getStartupTaskResult result = " + result);
171    windowStage.loadContent('pages/Index', (err, data) => {
172      if (err.code) {
173        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
174        return;
175      }
176      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
177    });
178  }
179}
180```
181
182
183## startupManager.isStartupTaskInitialized
184
185isStartupTaskInitialized(startupTask: string): boolean
186
187Checks whether a startup task or .so file preloading task is initialized.
188
189**System capability**: SystemCapability.Ability.AppStartup
190
191**Parameters**
192
193  | Name| Type| Mandatory| Description|
194  | -------- | -------- | -------- | -------- |
195  | startupTask | string | Yes| Class name of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task or .so file name.|
196
197**Return value**
198
199  | Type| Description|
200  | -------- | -------- |
201  | boolean | Check result. The value **true** means that the task is initialized, and **false** means the opposite.|
202
203**Error codes**
204
205For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
206
207  | ID| Error Message|
208  | ------- | -------------------------------- |
209  | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
210
211**Example**
212
213```ts
214import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit';
215import { window } from '@kit.ArkUI';
216import { hilog } from '@kit.PerformanceAnalysisKit';
217
218export default class EntryAbility extends UIAbility {
219  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
220    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
221    startupManager.run(["StartupTask_001", "libentry_001"]).then(() => {
222      console.info("StartupTask_001 init successful");
223    })
224  }
225
226  onWindowStageCreate(windowStage: window.WindowStage) {
227    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
228    let result1 = startupManager.isStartupTaskInitialized('StartupTask_001');
229    let result2 = startupManager.isStartupTaskInitialized('libentry_001');
230    if (result1) {
231      console.info("StartupTask_001 init successful");
232    } else {
233      console.info("StartupTask_001 uninitialized");
234    }
235    if (result2) {
236      console.info("libentry_001 init successful");
237    } else {
238      console.info("libentry_001 uninitialized");
239    }
240
241    windowStage.loadContent('pages/Index', (err, data) => {
242      if (err.code) {
243        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
244        return;
245      }
246      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
247    });
248  }
249}
250```
251
252## startupManager.removeStartupTaskResult
253
254removeStartupTaskResult(startupTask: string): void
255
256Removes the initialization result of a startup task or .so file preloading task.
257
258- If a startup task name is passed, the initialization result of that startup task is removed.
259
260- If a .so file is passed, the .so file is set to the unloaded state, but the loaded .so file in the cache is not removed.
261
262**System capability**: SystemCapability.Ability.AppStartup
263
264**Parameters**
265
266  | Name| Type| Mandatory| Description|
267  | -------- | -------- | -------- | -------- |
268  | startupTask | string | Yes| Class name of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task or .so file name.|
269
270**Error codes**
271
272For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
273
274  | ID| Error Message|
275  | ------- | -------------------------------- |
276  | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
277
278**Example**
279
280```ts
281import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit';
282import { window } from '@kit.ArkUI';
283import { hilog } from '@kit.PerformanceAnalysisKit';
284
285export default class EntryAbility extends UIAbility {
286  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
287    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
288    startupManager.run(["StartupTask_001", "libentry_001"]).then(() => {
289      console.info("StartupTask_001 init successful");
290    })
291  }
292
293  onWindowStageCreate(windowStage: window.WindowStage) {
294    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
295    startupManager.removeStartupTaskResult("StartupTask_001");
296    startupManager.removeStartupTaskResult("libentry_001");
297
298    windowStage.loadContent('pages/Index', (err, data) => {
299      if (err.code) {
300        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
301        return;
302      }
303      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
304    });
305  }
306}
307```
308