1# @ohos.app.appstartup.startupManager (AppStartup Management) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @yzkp--> 5<!--Designer: @yzkp--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9The module provides APIs to manage startup tasks in AppStartup. It can be called only in the main thread. 10 11> **NOTE** 12> 13> 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. 14> 15> This module supports .so file preloading since API version 18. 16> 17> The APIs of this module can be used only in the stage model. 18 19## Modules to Import 20 21```ts 22import { startupManager } from '@kit.AbilityKit'; 23``` 24 25## startupManager.run 26run(startupTasks: Array\<string\>, config?: StartupConfig): Promise\<void\> 27 28Runs startup tasks or loads .so files. 29 30> **NOTE** 31> 32> To run startup tasks in a feature HAP, use the [startupManager.run](#startupmanagerrun20) API. 33 34**System capability**: SystemCapability.Ability.AppStartup 35 36**Parameters** 37 38| Name| Type| Mandatory| Description| 39| -------- | -------- | -------- | -------- | 40| startupTasks | Array\<string\> | Yes| Array of [StartupTask](js-apis-app-appstartup-startupTask.md) names and names of .so files to be preloaded.| 41| config | [StartupConfig](./js-apis-app-appstartup-startupConfig.md) | No| Configuration for the AppStartup timeout and startup task listener.| 42 43**Return value** 44 45| Type| Description| 46| -------- | -------- | 47| Promise\<void\> | Promise that returns no value.| 48 49**Error codes** 50 51For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 52 53 | ID| Error Message| 54 | ------- | -------------------------------- | 55 | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 56 | 16000050 | Internal error. | 57 | 28800001 | Startup task or its dependency not found. | 58 | 28800002 | The startup tasks have circular dependencies. | 59 | 28800003 | An error occurred while running the startup tasks. | 60 | 28800004 | Running startup tasks timeout. | 61 62**Example** 63 64```ts 65import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit'; 66import { hilog } from '@kit.PerformanceAnalysisKit'; 67import { BusinessError } from '@kit.BasicServicesKit'; 68 69export default class EntryAbility extends UIAbility { 70 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 71 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 72 let startParams = ['StartupTask_001', 'libentry_001']; 73 try { 74 // Manually call the run method. 75 startupManager.run(startParams).then(() => { 76 console.log(`StartupTest startupManager run then, startParams = ${startParams}.`); 77 }).catch((error: BusinessError) => { 78 console.error(`StartupTest promise catch failed, error code: ${error.code}, error msg: ${error.message}.`); 79 }); 80 } catch (error) { 81 let errMsg = (error as BusinessError).message; 82 let errCode = (error as BusinessError).code; 83 console.error(`Startup.run failed, err code: ${errCode}, err msg: ${errMsg}.`); 84 } 85 } 86 87 // ... 88} 89``` 90 91## startupManager.run<sup>20+</sup> 92 93run(startupTasks: Array\<string\>, context: common.AbilityStageContext, config: StartupConfig): Promise\<void\> 94 95Runs startup tasks or loads .so files. You can specify [AbilityStageContext](js-apis-inner-application-abilityStageContext.md) for loading startup tasks. This API uses a promise to return the result. 96 97**System capability**: SystemCapability.Ability.AppStartup 98 99**Parameters** 100 101| Name | Type | Mandatory| Description | 102| ------------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 103| startupTasks | Array\<string\> | Yes | Array of [StartupTask](js-apis-app-appstartup-startupTask.md) names and names of .so files to be preloaded.| 104| context | [common.AbilityStageContext](js-apis-inner-application-abilityStageContext.md) | Yes | AbilityStage context that executes the [StartupTask](js-apis-app-appstartup-startupTask.md). It is passed as an input parameter to [init](js-apis-app-appstartup-startupTask.md#init) of the task.| 105| config | [StartupConfig](./js-apis-app-appstartup-startupConfig.md) | Yes | Configuration for the AppStartup timeout and startup task listener. | 106 107**Return value** 108 109| Type | Description | 110| --------------- | -------------------------------------- | 111| Promise\<void\> | Promise that returns no value.| 112 113**Error codes** 114 115For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 116 117| ID| Error Message | 118| -------- | -------------------------------------------------- | 119| 16000050 | Internal error. | 120| 28800001 | Startup task or its dependency not found. | 121| 28800002 | The startup tasks have circular dependencies. | 122| 28800003 | An error occurred while running the startup tasks. | 123| 28800004 | Running startup tasks timeout. | 124 125**Example** 126 127```ts 128import { AbilityStage, startupManager, StartupListener, StartupConfig } from '@kit.AbilityKit'; 129import { hilog } from '@kit.PerformanceAnalysisKit'; 130import { BusinessError } from '@kit.BasicServicesKit'; 131 132export default class MyAbilityStage extends AbilityStage { 133 onCreate(): void { 134 hilog.info(0x0000, 'testTag', 'AbilityStage onCreate'); 135 let onCompletedCallback = (error: BusinessError<void>) => { 136 if (error) { 137 hilog.error(0x0000, 'testTag', 'onCompletedCallback error: %{public}s', JSON.stringify(error)); 138 } else { 139 hilog.info(0x0000, 'testTag', 'onCompletedCallback: success.'); 140 } 141 }; 142 let startupListener: StartupListener = { 143 'onCompleted': onCompletedCallback 144 }; 145 let config: StartupConfig = { 146 'timeoutMs': 10000, 147 'startupListener': startupListener 148 }; 149 150 try { 151 // Manually call the run method. 152 startupManager.run(["StartupTask_001", "libentry_001"], this.context, config).then(() => { 153 hilog.info(0x0000, 'testTag', '%{public}s', 'startupManager.run success'); 154 }).catch((error: BusinessError<void>) => { 155 hilog.error(0x0000, 'testTag', 'startupManager.run promise catch error: %{public}s', JSON.stringify(error)); 156 }) 157 } catch (error) { 158 hilog.error(0x0000, 'testTag', 'startupManager.run catch error: %{public}s', JSON.stringify(error)); 159 } 160 } 161 // ... 162} 163``` 164 165## startupManager.removeAllStartupTaskResults 166 167removeAllStartupTaskResults(): void 168 169Removes all startup task results. 170 171If 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. 172 173**System capability**: SystemCapability.Ability.AppStartup 174 175**Example** 176 177```ts 178import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit'; 179import { window } from '@kit.ArkUI'; 180import { hilog } from '@kit.PerformanceAnalysisKit'; 181 182export default class EntryAbility extends UIAbility { 183 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 184 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 185 startupManager.run(["StartupTask_001", "libentry_001"]).then(() => { 186 console.info("StartupTask_001 init successful"); 187 }) 188 } 189 190 onWindowStageCreate(windowStage: window.WindowStage) { 191 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 192 startupManager.removeAllStartupTaskResults(); // Remove all startup task results. 193 194 windowStage.loadContent('pages/Index', (err, data) => { 195 if (err.code) { 196 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 197 return; 198 } 199 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 200 }); 201 } 202} 203``` 204 205 206## startupManager.getStartupTaskResult 207 208getStartupTaskResult(startupTask: string): Object 209 210Obtains the execution result of a startup task or .so file preloading task. 211 212**System capability**: SystemCapability.Ability.AppStartup 213 214**Parameters** 215 216 | Name| Type| Mandatory| Description| 217 | -------- | -------- | -------- | -------- | 218 | 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.| 219 220**Return value** 221 222 | Type| Description| 223 | -------- | -------- | 224 | Object | Execution result of the startup task if a startup task name is passed.<br> undefined if a .so file name is passed.| 225 226**Error codes** 227 228For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 229 230 | ID| Error Message| 231 | ------- | -------------------------------- | 232 | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 233 234**Example** 235 236```ts 237import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit'; 238import { window } from '@kit.ArkUI'; 239import { hilog } from '@kit.PerformanceAnalysisKit'; 240 241export default class EntryAbility extends UIAbility { 242 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 243 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 244 startupManager.run(["StartupTask_001"]).then(() => { 245 console.info("StartupTask_001 init successful"); 246 }) 247 } 248 249 onWindowStageCreate(windowStage: window.WindowStage) { 250 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 251 let result = startupManager.getStartupTaskResult("StartupTask_001"); // Manually obtain the startup task result. 252 console.info("getStartupTaskResult result = " + result); 253 windowStage.loadContent('pages/Index', (err, data) => { 254 if (err.code) { 255 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 256 return; 257 } 258 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 259 }); 260 } 261} 262``` 263 264 265## startupManager.isStartupTaskInitialized 266 267isStartupTaskInitialized(startupTask: string): boolean 268 269Checks whether a startup task or .so file preloading task is initialized. 270 271**System capability**: SystemCapability.Ability.AppStartup 272 273**Parameters** 274 275 | Name| Type| Mandatory| Description| 276 | -------- | -------- | -------- | -------- | 277 | startupTask | string | Yes| Class name of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task or .so file name.| 278 279**Return value** 280 281 | Type| Description| 282 | -------- | -------- | 283 | boolean | Check result for whether the task is initialized. **true** if initialized, **false** otherwise.| 284 285**Error codes** 286 287For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 288 289 | ID| Error Message| 290 | ------- | -------------------------------- | 291 | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 292 293**Example** 294 295```ts 296import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit'; 297import { window } from '@kit.ArkUI'; 298import { hilog } from '@kit.PerformanceAnalysisKit'; 299 300export default class EntryAbility extends UIAbility { 301 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 302 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 303 startupManager.run(["StartupTask_001", "libentry_001"]).then(() => { 304 console.info("StartupTask_001 init successful"); 305 }) 306 } 307 308 onWindowStageCreate(windowStage: window.WindowStage) { 309 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 310 let result1 = startupManager.isStartupTaskInitialized('StartupTask_001'); 311 let result2 = startupManager.isStartupTaskInitialized('libentry_001'); 312 if (result1) { 313 console.info("StartupTask_001 init successful"); 314 } else { 315 console.info("StartupTask_001 uninitialized"); 316 } 317 if (result2) { 318 console.info("libentry_001 init successful"); 319 } else { 320 console.info("libentry_001 uninitialized"); 321 } 322 323 windowStage.loadContent('pages/Index', (err, data) => { 324 if (err.code) { 325 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 326 return; 327 } 328 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 329 }); 330 } 331} 332``` 333 334## startupManager.removeStartupTaskResult 335 336removeStartupTaskResult(startupTask: string): void 337 338Removes the initialization result of a startup task or .so file preloading task. 339 340- If a startup task name is passed, the initialization result of that startup task is removed. 341 342- 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. 343 344**System capability**: SystemCapability.Ability.AppStartup 345 346**Parameters** 347 348 | Name| Type| Mandatory| Description| 349 | -------- | -------- | -------- | -------- | 350 | startupTask | string | Yes| Class name of the [StartupTask](js-apis-app-appstartup-startupTask.md) API implemented by the startup task or .so file name.| 351 352**Error codes** 353 354For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 355 356 | ID| Error Message| 357 | ------- | -------------------------------- | 358 | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 359 360**Example** 361 362```ts 363import { AbilityConstant, UIAbility, Want, startupManager } from '@kit.AbilityKit'; 364import { window } from '@kit.ArkUI'; 365import { hilog } from '@kit.PerformanceAnalysisKit'; 366 367export default class EntryAbility extends UIAbility { 368 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 369 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 370 startupManager.run(["StartupTask_001", "libentry_001"]).then(() => { 371 console.info("StartupTask_001 init successful"); 372 }) 373 } 374 375 onWindowStageCreate(windowStage: window.WindowStage) { 376 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 377 startupManager.removeStartupTaskResult("StartupTask_001"); 378 startupManager.removeStartupTaskResult("libentry_001"); 379 380 windowStage.loadContent('pages/Index', (err, data) => { 381 if (err.code) { 382 hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 383 return; 384 } 385 hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 386 }); 387 } 388} 389``` 390