启动框架
应用启动时需要初始化相关方法,如果都放在Ability的生命周期中会影响启动速度,当需要初始化的方法过多并且之间存在依赖关系会导致代码难以维护。通过配置启动框架可在异步线程中进行初始化方法并且方法之间结果可依次传递依赖关系更加明确。
添加配置和相关依赖文件
1、module.json5的module中添加appStartup指向配置文件
json
{
"module": {
"appStartup": "$profile:startup_config"
}
}
2、resources/base/profile资源目录下新建startup_config.json
json
{
"configEntry": "./ets/startup/StartConfig.ets",
"startupTasks": [
{
"name": "demo01",
"srcEntry": "./ets/startup/demo01.ets",
"dependencies": [],
"runOnThread": "taskPool",
"waitOnMainThread": false
}
]
}
- configEntry:管理所有启动任务,可以监听是否成功和任务超时时间
- startupTasks:
- name:任务名称唯一标识
- srcEntry:具体的启动任务方法实现
- dependencies:当前的启动任务依赖的name
- runOnThread:当前的启动任务是在主线程或异步线程中执行
- waitOnMainThread:当前启动任务为异步时,是否等待执行完成然后加载首页
3、ets目录下新建startup目录,存放所有启动任务
ts
// ets/startup/StartConfig.ets
import { StartupConfig, StartupConfigEntry } from '@kit.AbilityKit';
export default class StartConfig extends StartupConfigEntry {
onConfig(): StartupConfig {
const config:StartupConfig = {
timeoutMs: 1000,
startupListener: {
onCompleted(err) {
console.info(JSON.stringify(err))
}
}
}
return config
}
}
- timeoutMs:所有启动任务执行完成限制时间
- startupListener:所有启动任务执行完成调用。执行完成超出timeoutMs时或执行期间任务报错时可获取报错信息
4、具体的启动任务
ts
import { common, StartupTask } from '@kit.AbilityKit'
@Sendable
export default class Demo01 extends StartupTask {
constructor() {
super()
}
async init(context: common.AbilityStageContext): Promise<void | Object> {
// 启动任务实现...
console.info('demo01启动')
return
}
}
执行顺序:
1、runOnThread为mainThread或taskPool,waitOnMainThread为true时
demo01 -> AbilityStage oncreate -> UIAbility oncreate
AbilityStage为module级别的容器,hap首次加载时执行,可对module初始化。
2、runOnThread为mainThread,waitOnMainThread为false时
AbilityStage oncreate -> demo01 -> UIAbility oncreate
3、runOnThread为taskPool,waitOnMainThread为false时
AbilityStage oncreate -> UIAbility oncreate -> onWindowStageCreate -> demo01
启动任务相互依赖
demo02依赖demo01的执行结果相关配置
json
{
"configEntry": "./ets/startup/StartConfig.ets",
"startupTasks": [
{
"name": "demo01",
"srcEntry": "./ets/startup/demo01.ets",
"dependencies": [],
"runOnThread": "mainThread"
},
{
"name": "demo02",
"srcEntry": "./ets/startup/demo02.ets",
"dependencies": [
"demo01"
],
"runOnThread": "mainThread"
}
]
}
demo01
ts
import { common, StartupTask } from '@kit.AbilityKit'
@Sendable
export default class Demo01 extends StartupTask {
constructor() {
super()
}
async init(context: common.AbilityStageContext): Promise<void | Object> {
console.info('demo01启动执行')
const result = 'hello'
return result // demo01的执行结果
}
}
demo02
ts
import { common, StartupTask } from '@kit.AbilityKit'
@Sendable
export default class Demo02 extends StartupTask {
constructor() {
super()
}
private str: string = ''
async init(context: common.AbilityStageContext): Promise<void | Object> {
console.info('demo02启动执行')
console.info(this.str + ' world') // hello world
return
}
onDependencyCompleted(dependency: string, result: Object): void {
console.info(dependency) // 依赖任务的name demo01
console.info(JSON.stringify(result)) // 依赖任务的return内容 hello
this.str = result as string
}
}
手动模式
启动框架默认为自动执行任务,通过配置也可手动执行
json
{
"configEntry": "./ets/startup/StartConfig.ets",
"startupTasks": [
{
"name": "demo01",
"srcEntry": "./ets/startup/demo01.ets",
"dependencies": [],
"runOnThread": "mainThread",
"excludeFromAutoStart": true
}
]
}
- excludeFromAutoStart:是否手动执行
ts
Button('按钮').onClick(() => {
startupManager.run(['demo01'])
})