{
"startupTasks": [//有哪些任务
{
"name": "StartupTask_001",//任务名字
"srcEntry": "./ets/startup/StartupTask_001.ets",//任务的文件路径
"runOnThread": "taskpool",//运行在哪个现成,有两个选择,任务池或者主线程
"waitOnMainThread": false //要不要主线程等待该任务,一般是false
},
{
"name": "StartupTask_002",
"srcEntry": "./ets/startup/StartupTask_002.ets",
"dependencies": [//是否依赖其它的任务
"StartupTask_001"
],
"runOnThread": "taskpool",
"waitOnMainThread": false
},
{
"name": "StartupTask_003",
"srcEntry": "./ets/startup/StartupTask_003.ets",
"dependencies": [
"StartupTask_001"
],
"runOnThread": "taskpool",
"waitOnMainThread": false
},
{
"name": "StartupTask_004",
"srcEntry": "./ets/startup/StartupTask_004.ets",
"dependencies": [
"StartupTask_002"
],
"runOnThread": "taskpool",
"waitOnMainThread": false
},
{
"name": "StartupTask_005",
"srcEntry": "./ets/startup/StartupTask_005.ets",
"dependencies": [
"StartupTask_003",
"StartupTask_004"
],
"runOnThread": "taskpool",
"waitOnMainThread": false
},
{
"name": "StartupTask_006",
"srcEntry": "./ets/startup/StartupTask_006.ets",
"runOnThread": "mainThread",
"waitOnMainThread": false,
"excludeFromAutoStart": true
},
{
"name": "StartupTask_007",
"srcEntry": "./ets/startup/StartupTask_007.ets",
"dependencies": [
"StartupTask_006"
],
"runOnThread": "mainThread",
"waitOnMainThread": false,
"excludeFromAutoStart": true //false表示系统加载的时候,自动执行任务,true表示可以在代码中任何地方执行
}
],
"configEntry": "./ets/startup/MyStartupConfigEntry.ets"
}
举例:在Ability中执行任务6 和 7
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
let startParams = ['StartupTask_006', 'StartupTask_007'];
try {
startupManager.run(startParams).then(() => {
console.log('StartupTest startupManager run then, startParams = ');
}).catch((error: BusinessError) => {
console.info("StartupTest promise catch error, error = " + JSON.stringify(error));
console.info("StartupTest promise catch error, startParams = "
+ JSON.stringify(startParams));
})
} catch (error) {
let errMsg = JSON.stringify(error);
let errCode: number = error.code;
console.log('Startup catch error , errCode= ' + errCode);
console.log('Startup catch error ,error= ' + errMsg);
}
}
上面代码是startup_config的讲解,下面是如何操作
在如上位置增加startup_config.json,内容如下
{
"startupTasks": [
{
"name": "StartupTask_001",
"srcEntry": "./ets/startup/InitMMKV.ets",
"runOnThread": "taskPool",
"waitOnMainThread": false
},
{
"name": "StartupTask_002",
"srcEntry": "./ets/startup/initZRouter.ets",
"dependencies": [
"StartupTask_001"
],
"runOnThread": "taskPool",
"waitOnMainThread": false
},
{
"name": "StartupTask_003",
"srcEntry": "./ets/startup/StartupTask_003.ets",
"dependencies": [
"StartupTask_001"
],
"runOnThread": "taskPool",
"waitOnMainThread": false
},
{
"name": "StartupTask_004",
"srcEntry": "./ets/startup/StartupTask_004.ets",
"dependencies": [
"StartupTask_002"
],
"runOnThread": "taskPool",
"waitOnMainThread": false
},
{
"name": "StartupTask_005",
"srcEntry": "./ets/startup/StartupTask_005.ets",
"dependencies": [
"StartupTask_003",
"StartupTask_004"
],
"runOnThread": "taskPool",
"waitOnMainThread": false
},
{
"name": "StartupTask_006",
"srcEntry": "./ets/startup/StartupTask_006.ets",
"runOnThread": "mainThread",
"waitOnMainThread": false,
"excludeFromAutoStart": true
},
{
"name": "StartupTask_007",
"srcEntry": "./ets/startup/StartupTask_007.ets",
"dependencies": [
"StartupTask_006"
],
"runOnThread": "mainThread",
"waitOnMainThread": false,
"excludeFromAutoStart": true
}
],
"configEntry": "./ets/startup/StartUpConfigEntry.ets"
}
StartUpConfigEntry.ets文件是配置监听,监听任务的执行,代码如下
//配置监听,监听任务的执行
import { StartupConfig, StartupConfigEntry, StartupListener } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class StartUpConfigEntry extends StartupConfigEntry {
onConfig() {
hilog.info(0x0000, 'testTag', `onConfig`);
//任务执行完成的回调,监听所有的组
let onCompletedCallback = (error: BusinessError<void>) => {
hilog.info(0x0000, 'testTag', `onCompletedCallback`);
if (error) {
hilog.info(0x0000, 'testTag', 'onCompletedCallback: %{public}d, message: %{public}s', error.code, error.message);
} else {
hilog.info(0x0000, 'testTag', `onCompletedCallback: success.`);
}
};
let startupListener: StartupListener = {
'onCompleted': onCompletedCallback
};
let config: StartupConfig = {
'timeoutMs': 10000,//10秒钟之内执行失败,进入error回调方法
'startupListener': startupListener
};
return config;
}
}
startup文件夹下的其余的文件就是我们配置的启动任务,我们选择其中的两个,
import { StartupTask, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
/**
* 启动任务。需要继承StartupTask
*/
@Sendable
export default class StartupTask_001 extends StartupTask {
constructor() {
super();
}
async init(context: common.AbilityStageContext) {
console.info('testTagStartUP: ', '任务一:实例化MMKV');
return 'initMMKV';//在下一个任务的onDependencyCompleted回调方法会传入StartupTask_001
}
onDependencyCompleted(dependence: string, result: Object): void {
hilog.info(0x0000, 'testTagStartUP', 'StartupTask_001 onDependencyCompleted, dependence: %{public}s, result: %{public}s',
dependence, JSON.stringify(result));
}
}
在init方法里初始化第三方SDK
import { StartupTask, common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Sendable
export default class StartupTask_007 extends StartupTask {
constructor() {
super();
}
async init(context: common.AbilityStageContext) {
hilog.info(0x0000, 'testTagStartUP', 'StartupTask_007 init.');
return 'StartupTask_007';
}
onDependencyCompleted(dependence: string, result: Object): void {
hilog.info(0x0000, 'testTagStartUP', 'StartupTask_007 onDependencyCompleted, dependence: %{public}s, result: %{public}s',
dependence, JSON.stringify(result));
}
}
在module.json5中配置(见下图)
OK大功告成。