1# @ohos.app.ability.missionManager (missionManager)(系统接口) 2 3missionManager模块提供系统任务管理能力,包括对系统任务执行锁定、解锁、清理、切换到前台等操作。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口均为系统接口,三方应用不支持调用。 10 11## 导入模块 12 13```ts 14import missionManager from '@ohos.app.ability.missionManager'; 15``` 16 17## 权限列表 18 19ohos.permission.MANAGE_MISSIONS 20 21## missionManager.on 22 23on(type:'mission', listener: MissionListener): number 24 25注册系统任务状态监听器。 26 27**需要权限**:ohos.permission.MANAGE_MISSIONS 28 29**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 30 31**系统API**: 此接口为系统接口,三方应用不支持调用。 32 33**参数:** 34 35 | 参数名 | 类型 | 必填 | 说明 | 36 | -------- | -------- | -------- | -------- | 37 | type | string | 是 | 监听的任务名称。 | 38 | listener | [MissionListener](js-apis-inner-application-missionListener-sys.md) | 是 | 系统任务监听器。 | 39 40**返回值:** 41 42 | 类型 | 说明 | 43 | -------- | -------- | 44 | number | 监听器的index值,由系统创建,在注册系统任务状态监听时分配,和监听器一一对应 。 | 45 46**示例:** 47 48```ts 49import missionManager from '@ohos.app.ability.missionManager'; 50import UIAbility from '@ohos.app.ability.UIAbility'; 51import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 52import common from '@ohos.app.ability.common'; 53import Want from '@ohos.app.ability.Want'; 54import { BusinessError } from '@ohos.base'; 55import window from '@ohos.window'; 56import image from '@ohos.multimedia.image'; 57 58let listener: missionManager.MissionListener = { 59 onMissionCreated: (mission: number) => {console.log('--------onMissionCreated-------');}, 60 onMissionDestroyed: (mission: number) => {console.log('--------onMissionDestroyed-------');}, 61 onMissionSnapshotChanged: (mission: number) => {console.log('--------onMissionSnapshotChanged-------');}, 62 onMissionMovedToFront: (mission: number) => {console.log('--------onMissionMovedToFront-------');}, 63 onMissionIconUpdated: (mission: number, icon: image.PixelMap) => {console.log('--------onMissionIconUpdated-------');}, 64 onMissionClosed: (mission: number) => {console.log('--------onMissionClosed-------');}, 65 onMissionLabelUpdated: (mission: number) => {console.log('--------onMissionLabelUpdated-------');} 66}; 67 68let listenerId = -1; 69let abilityWant: Want; 70let context: common.UIAbilityContext; 71 72export default class EntryAbility extends UIAbility { 73 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 74 console.log('[Demo] EntryAbility onCreate'); 75 abilityWant = want; 76 context = this.context; 77 } 78 79 onDestroy() { 80 try { 81 if (listenerId !== -1) { 82 missionManager.off('mission', listenerId).catch((err: BusinessError) => { 83 console.log(JSON.stringify(err)); 84 }); 85 } 86 } catch (paramError) { 87 let code = (paramError as BusinessError).code; 88 let message = (paramError as BusinessError).message; 89 console.error(`error: ${code}, ${message} `); 90 } 91 console.log('[Demo] EntryAbility onDestroy'); 92 } 93 94 onWindowStageCreate(windowStage: window.WindowStage) { 95 // Main window is created, set main page for this ability 96 console.log('[Demo] EntryAbility onWindowStageCreate'); 97 try { 98 listenerId = missionManager.on('mission', listener); 99 } catch (paramError) { 100 let code = (paramError as BusinessError).code; 101 let message = (paramError as BusinessError).message; 102 console.error(`error: ${code}, ${message} `); 103 } 104 105 windowStage.loadContent('pages/index', (err, data) => { 106 if (err.code) { 107 console.error(`Failed to load the content. Cause: ${JSON.stringify(err)}`); 108 return; 109 } 110 console.info(`Succeeded in loading the content. Data: ${JSON.stringify(data)}`); 111 }); 112 } 113}; 114``` 115 116 117## missionManager.off 118 119off(type: 'mission', listenerId: number, callback: AsyncCallback<void>): void 120 121解注册任务状态监听器。 122 123**需要权限**:ohos.permission.MANAGE_MISSIONS 124 125**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 126 127**系统API**: 此接口为系统接口,三方应用不支持调用。 128 129**参数:** 130 131 | 参数名 | 类型 | 必填 | 说明 | 132 | -------- | -------- | -------- | -------- | 133 | type | string | 是 | 取消监听的任务名称。 | 134 | listenerId | number | 是 | 系统任务状态监器法的index值,和监听器一一对应,由on方法返回。 | 135 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 136 137**错误码**: 138 139| 错误码ID | 错误信息 | 140| ------- | -------- | 141| 16300002 | Input error. The specified mission listener does not exist. | 142 143以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 144 145**示例:** 146 147```ts 148import missionManager from '@ohos.app.ability.missionManager'; 149import UIAbility from '@ohos.app.ability.UIAbility'; 150import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 151import common from '@ohos.app.ability.common'; 152import Want from '@ohos.app.ability.Want'; 153import { BusinessError } from '@ohos.base'; 154import window from '@ohos.window'; 155import image from '@ohos.multimedia.image'; 156 157let listener: missionManager.MissionListener = { 158 onMissionCreated: (mission: number) => {console.log('--------onMissionCreated-------');}, 159 onMissionDestroyed: (mission: number) => {console.log('--------onMissionDestroyed-------');}, 160 onMissionSnapshotChanged: (mission: number) => {console.log('--------onMissionSnapshotChanged-------');}, 161 onMissionMovedToFront: (mission: number) => {console.log('--------onMissionMovedToFront-------');}, 162 onMissionIconUpdated: (mission: number, icon: image.PixelMap) => {console.log('--------onMissionIconUpdated-------');}, 163 onMissionClosed: (mission: number) => {console.log('--------onMissionClosed-------');}, 164 onMissionLabelUpdated: (mission: number) => {console.log('--------onMissionLabelUpdated-------');} 165}; 166 167let listenerId = -1; 168let abilityWant: Want; 169let context: common.UIAbilityContext; 170 171export default class EntryAbility extends UIAbility { 172 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 173 console.log('[Demo] EntryAbility onCreate'); 174 abilityWant = want; 175 context = this.context; 176 } 177 178 onDestroy() { 179 try { 180 if (listenerId !== -1) { 181 missionManager.off('mission', listenerId, (err: BusinessError) => { 182 console.log(`${err.code}`); 183 }); 184 } 185 } catch (paramError) { 186 let code = (paramError as BusinessError).code; 187 let message = (paramError as BusinessError).message; 188 console.error(`error: ${code}, ${message} `); 189 } 190 console.log('[Demo] EntryAbility onDestroy'); 191 } 192 193 onWindowStageCreate(windowStage: window.WindowStage) { 194 // Main window is created, set main page for this ability 195 console.log('[Demo] EntryAbility onWindowStageCreate'); 196 try { 197 listenerId = missionManager.on('mission', listener); 198 } catch (paramError) { 199 let code = (paramError as BusinessError).code; 200 let message = (paramError as BusinessError).message; 201 console.error(`error: ${code}, ${message} `); 202 } 203 204 windowStage.loadContent('pages/index', (err: BusinessError, data) => { 205 if (err.code) { 206 console.error(`Failed to load the content. Cause: ${JSON.stringify(err)}`); 207 return; 208 } 209 console.info(`Succeeded in loading the content. Data: ${JSON.stringify(data)}`); 210 }); 211 } 212}; 213``` 214 215 216## missionManager.off 217 218off(type: 'mission', listenerId: number): Promise<void> 219 220解注册任务状态监听,以promise方式返回执行结果。 221 222**需要权限**:ohos.permission.MANAGE_MISSIONS 223 224**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 225 226**系统API**: 此接口为系统接口,三方应用不支持调用。 227 228**参数:** 229 230 | 参数名 | 类型 | 必填 | 说明 | 231 | -------- | -------- | -------- | -------- | 232 | type | string | 是 | 取消监听的任务名称。 | 233 | listenerId | number | 是 | 系统任务状态监听器的index值,和监听器一一对应,由on方法返回。 | 234 235**返回值:** 236 237 | 类型 | 说明 | 238 | -------- | -------- | 239 | Promise<void> | promise方式返回执行结果。 | 240 241**错误码**: 242 243| 错误码ID | 错误信息 | 244| ------- | -------- | 245| 16300002 | Input error. The specified mission listener does not exist. | 246 247以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 248 249**示例:** 250 251```ts 252import missionManager from '@ohos.app.ability.missionManager'; 253import UIAbility from '@ohos.app.ability.UIAbility'; 254import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 255import common from '@ohos.app.ability.common'; 256import Want from '@ohos.app.ability.Want'; 257import { BusinessError } from '@ohos.base'; 258import window from '@ohos.window'; 259import image from '@ohos.multimedia.image'; 260 261let listener: missionManager.MissionListener = { 262 onMissionCreated: (mission: number) => {console.log('--------onMissionCreated-------');}, 263 onMissionDestroyed: (mission: number) => {console.log('--------onMissionDestroyed-------');}, 264 onMissionSnapshotChanged: (mission: number) => {console.log('--------onMissionSnapshotChanged-------');}, 265 onMissionMovedToFront: (mission: number) => {console.log('--------onMissionMovedToFront-------');}, 266 onMissionIconUpdated: (mission: number, icon: image.PixelMap) => {console.log('--------onMissionIconUpdated-------');}, 267 onMissionClosed: (mission: number) => {console.log('--------onMissionClosed-------');}, 268 onMissionLabelUpdated: (mission: number) => {console.log('--------onMissionLabelUpdated-------');} 269}; 270 271let listenerId = -1; 272let abilityWant: Want; 273let context: common.UIAbilityContext; 274 275export default class EntryAbility extends UIAbility { 276 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 277 console.log('[Demo] EntryAbility onCreate'); 278 abilityWant = want; 279 context = this.context; 280 } 281 282 onDestroy() { 283 try { 284 if (listenerId !== -1) { 285 missionManager.off('mission', listenerId).catch((err: BusinessError) => { 286 console.log(`${err.code}`); 287 }); 288 } 289 } catch (paramError) { 290 let code = (paramError as BusinessError).code; 291 let message = (paramError as BusinessError).message; 292 console.error(`error: ${code}, ${message} `); 293 } 294 console.log('[Demo] EntryAbility onDestroy'); 295 } 296 297 onWindowStageCreate(windowStage: window.WindowStage) { 298 // Main window is created, set main page for this ability 299 console.log('[Demo] EntryAbility onWindowStageCreate'); 300 try { 301 listenerId = missionManager.on('mission', listener); 302 } catch (paramError) { 303 let code = (paramError as BusinessError).code; 304 let message = (paramError as BusinessError).message; 305 console.error(`error: ${code}, ${message} `); 306 } 307 308 windowStage.loadContent('pages/index', (err: BusinessError, data) => { 309 if (err.code) { 310 console.error(`Failed to load the content. Cause: ${JSON.stringify(err)}`); 311 return; 312 } 313 console.info(`Succeeded in loading the content. Data: ${JSON.stringify(data)}`); 314 }); 315 } 316}; 317``` 318 319## missionManager.getMissionInfo 320 321getMissionInfo(deviceId: string, missionId: number, callback: AsyncCallback<MissionInfo>): void 322 323获取任务信息,以异步回调的方式返回任务信息。 324 325**需要权限**:ohos.permission.MANAGE_MISSIONS 326 327**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 328 329**系统API**: 此接口为系统接口,三方应用不支持调用。 330 331**参数:** 332 333 | 参数名 | 类型 | 必填 | 说明 | 334 | -------- | -------- | -------- | -------- | 335 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 336 | missionId | number | 是 | 任务ID。 | 337 | callback | AsyncCallback<[MissionInfo](js-apis-inner-application-missionInfo-sys.md)> | 是 | 执行结果回调函数,返回任务信息。 | 338 339**示例:** 340 341 ```ts 342 import missionManager from '@ohos.app.ability.missionManager'; 343 import { BusinessError } from '@ohos.base'; 344 345 let testMissionId = 1; 346 347 missionManager.getMissionInfos('',10) 348 .then((allMissions: Array<missionManager.MissionInfo>) => { 349 try { 350 if (allMissions && allMissions.length > 0) { 351 testMissionId = allMissions[0].missionId; 352 } 353 354 missionManager.getMissionInfo('', testMissionId, (error: BusinessError, mission: missionManager.MissionInfo) => { 355 if (error) { 356 console.error(`getMissionInfo failed, error.code: ${error.code}, error.message: ${error.message}`); 357 } else { 358 console.log(`mission.missionId = ${mission.missionId}`); 359 console.log(`mission.runningState = ${mission.runningState}`); 360 console.log(`mission.lockedState = ${mission.lockedState}`); 361 console.log(`mission.timestamp = ${mission.timestamp}`); 362 console.log(`mission.label = ${mission.label}`); 363 console.log(`mission.iconPath = ${mission.iconPath}`); 364 } 365 }); 366 } catch (paramError) { 367 let code = (paramError as BusinessError).code; 368 let message = (paramError as BusinessError).message; 369 console.error(`error: ${code}, ${message} `); 370 } 371 }) 372 .catch((err: BusinessError) => {console.log(`${err.code}`);}); 373 ``` 374 375## missionManager.getMissionInfo 376 377getMissionInfo(deviceId: string, missionId: number): Promise<MissionInfo> 378 379获取任务信息,以promise方式返回任务信息。 380 381**需要权限**:ohos.permission.MANAGE_MISSIONS 382 383**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 384 385**系统API**: 此接口为系统接口,三方应用不支持调用。 386 387**参数:** 388 389 | 参数名 | 类型 | 必填 | 说明 | 390 | -------- | -------- | -------- | -------- | 391 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 392 | missionId | number | 是 | 任务ID。 | 393 394**返回值:** 395 396 | 类型 | 说明 | 397 | -------- | -------- | 398 | Promise<[MissionInfo](js-apis-inner-application-missionInfo-sys.md)> | 任务信息。 | 399 400**示例:** 401 402```ts 403import missionManager from '@ohos.app.ability.missionManager'; 404import { BusinessError } from '@ohos.base'; 405 406let testMissionId = 1; 407try { 408 missionManager.getMissionInfo('', testMissionId).then((data: missionManager.MissionInfo) => { 409 console.info(`getMissionInfo successfully. Data: ${JSON.stringify(data)}`); 410 }).catch((error: BusinessError) => { 411 console.error(`getMissionInfo failed. Cause: ${error.message}`); 412 }); 413} catch (error) { 414 let err: BusinessError = error as BusinessError; 415 console.error(`getMissionInfo failed. Cause: ${err.message}`); 416} 417``` 418 419## missionManager.getMissionInfos 420 421getMissionInfos(deviceId: string, numMax: number, callback: AsyncCallback<Array<MissionInfo>>): void 422 423获取所有任务信息,以回调函数的方式返回任务信息数组。 424 425**需要权限**:ohos.permission.MANAGE_MISSIONS 426 427**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 428 429**系统API**: 此接口为系统接口,三方应用不支持调用。 430 431**参数:** 432 433 | 参数名 | 类型 | 必填 | 说明 | 434 | -------- | -------- | -------- | -------- | 435 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 436 | numMax | number | 是 | 任务信息数量上限。 | 437 | callback | AsyncCallback<Array<[MissionInfo](js-apis-inner-application-missionInfo-sys.md)>> | 是 | 执行结果回调函数,返回任务信息数组。 | 438 439**示例:** 440 441 ```ts 442 import missionManager from '@ohos.app.ability.missionManager'; 443 import { BusinessError } from '@ohos.base'; 444 445 try { 446 missionManager.getMissionInfos('', 10, (error: BusinessError, missions: Array<missionManager.MissionInfo>) => { 447 if (error) { 448 console.error(`getMissionInfos failed, error.code: ${error.code}, error.message: ${error.message}`); 449 } else { 450 console.log(`size = ${missions.length}`); 451 console.log(`missions = ${JSON.stringify(missions)}`); 452 } 453 }); 454 } catch (paramError) { 455 let code = (paramError as BusinessError).code; 456 let message = (paramError as BusinessError).message; 457 console.error(`error: ${code}, ${message} `); 458 } 459 ``` 460 461 462## missionManager.getMissionInfos 463 464getMissionInfos(deviceId: string, numMax: number): Promise<Array<MissionInfo>> 465 466获取所有任务信息,以promise的方式返回任务信息数组。 467 468**需要权限**:ohos.permission.MANAGE_MISSIONS 469 470**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 471 472**系统API**: 此接口为系统接口,三方应用不支持调用。 473 474**参数:** 475 476 | 参数名 | 类型 | 必填 | 说明 | 477 | -------- | -------- | -------- | -------- | 478 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 479 | numMax | number | 是 | 任务信息数量上限。 | 480 481**返回值:** 482 483 | 类型 | 说明 | 484 | -------- | -------- | 485 | Promise<Array<[MissionInfo](js-apis-inner-application-missionInfo-sys.md)>> | 任务信息数组。 | 486 487**示例:** 488 489```ts 490import missionManager from '@ohos.app.ability.missionManager'; 491import { BusinessError } from '@ohos.base'; 492 493try { 494 missionManager.getMissionInfos('', 10).then((data: Array<missionManager.MissionInfo>) => { 495 console.info(`getMissionInfos successfully. Data: ${JSON.stringify(data)}`); 496 }).catch((error: BusinessError) => { 497 console.error(`getMissionInfos failed. Cause: ${error.message}`); 498 }); 499} catch (error) { 500 let err: BusinessError = error as BusinessError; 501 console.error(`getMissionInfos failed. Cause: ${err.message}`); 502} 503``` 504 505## missionManager.getMissionSnapShot 506 507getMissionSnapShot(deviceId: string, missionId: number, callback: AsyncCallback<MissionSnapshot>): void 508 509获取任务快照,以回调函数的方式返回快照内容。 510 511**需要权限**:ohos.permission.MANAGE_MISSIONS 512 513**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 514 515**系统API**: 此接口为系统接口,三方应用不支持调用。 516 517**参数:** 518 519 | 参数名 | 类型 | 必填 | 说明 | 520 | -------- | -------- | -------- | -------- | 521 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 522 | missionId | number | 是 | 任务ID。 | 523 | callback | AsyncCallback<[MissionSnapshot](js-apis-inner-application-missionSnapshot-sys.md)> | 是 | 执行结果回调函数,返回任务快照信息。 | 524 525**示例:** 526```ts 527import missionManager from '@ohos.app.ability.missionManager'; 528import { BusinessError } from '@ohos.base'; 529 530let testMissionId = 2; 531try { 532 missionManager.getMissionSnapShot('', testMissionId, (err: BusinessError, data: missionManager.MissionSnapshot ) => { 533 if (err) { 534 console.error(`getMissionSnapShot failed: ${err.message}`); 535 } else { 536 console.info(`getMissionSnapShot successfully: ${JSON.stringify(data)}`); 537 } 538 }); 539} catch (error) { 540 let err: BusinessError = error as BusinessError; 541 console.error(`getMissionSnapShot failed: ${err.message}`); 542} 543``` 544 545## missionManager.getMissionSnapShot 546 547getMissionSnapShot(deviceId: string, missionId: number): Promise<MissionSnapshot> 548 549获取任务快照,以promise的方式返回快照内容。 550 551**需要权限**:ohos.permission.MANAGE_MISSIONS 552 553**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 554 555**系统API**: 此接口为系统接口,三方应用不支持调用。 556 557**参数:** 558 559 | 参数名 | 类型 | 必填 | 说明 | 560 | -------- | -------- | -------- | -------- | 561 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 562 | missionId | number | 是 | 任务ID。 | 563 564**返回值:** 565 566 | 类型 | 说明 | 567 | -------- | -------- | 568 | Promise<[MissionSnapshot](js-apis-inner-application-missionSnapshot-sys.md)> | 任务快照信息。 | 569 570**示例:** 571```ts 572import missionManager from '@ohos.app.ability.missionManager'; 573import { BusinessError } from '@ohos.base'; 574 575let testMissionId = 2; 576try { 577 missionManager.getMissionSnapShot('', testMissionId).then((data: missionManager.MissionSnapshot) => { 578 console.info(`getMissionSnapShot successfully. Data: ${JSON.stringify(data)}`); 579 }).catch((error: BusinessError) => { 580 console.error(`getMissionSnapShot failed. Cause: ${error.message}`); 581 }); 582} catch (error) { 583 let err: BusinessError = error as BusinessError; 584 console.error(`getMissionSnapShot failed. Cause: ${err.message}`); 585} 586``` 587 588## missionManager.getLowResolutionMissionSnapShot 589 590getLowResolutionMissionSnapShot(deviceId: string, missionId: number, callback: AsyncCallback\<MissionSnapshot>): void 591 592获取任务低分辨率快照。 593 594**需要权限**:ohos.permission.MANAGE_MISSIONS 595 596**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 597 598**系统API**: 此接口为系统接口,三方应用不支持调用。 599 600**参数:** 601 602 | 参数名 | 类型 | 必填 | 说明 | 603 | -------- | -------- | -------- | -------- | 604 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 605 | missionId | number | 是 | 任务ID。 | 606 | callback | AsyncCallback<[MissionSnapshot](js-apis-inner-application-missionSnapshot-sys.md)> | 是 | 执行结果回调函数,返回任务快照信息。 | 607 608**示例:** 609```ts 610import missionManager from '@ohos.app.ability.missionManager'; 611import { BusinessError } from '@ohos.base'; 612 613let testMissionId = 2; 614try { 615 missionManager.getLowResolutionMissionSnapShot('', testMissionId, (err: BusinessError, data: missionManager.MissionSnapshot) => { 616 if (err) { 617 console.error(`getLowResolutionMissionSnapShot failed: ${err.message}`); 618 } else { 619 console.info(`getLowResolutionMissionSnapShot successfully: ${JSON.stringify(data)}`); 620 } 621 }); 622} catch (error) { 623 let err: BusinessError = error as BusinessError; 624 console.error(`getLowResolutionMissionSnapShot failed: ${err.message}`); 625} 626``` 627 628## missionManager.getLowResolutionMissionSnapShot 629 630getLowResolutionMissionSnapShot(deviceId: string, missionId: number): Promise\<MissionSnapshot> 631 632获取任务低分辨率快照。 633 634**需要权限**:ohos.permission.MANAGE_MISSIONS 635 636**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 637 638**系统API**: 此接口为系统接口,三方应用不支持调用。 639 640**参数:** 641 642 | 参数名 | 类型 | 必填 | 说明 | 643 | -------- | -------- | -------- | -------- | 644 | deviceId | string | 是 | 设备ID,本机默认为空字符串。 | 645 | missionId | number | 是 | 任务ID。 | 646 647**返回值:** 648 649 | 类型 | 说明 | 650 | -------- | -------- | 651 | Promise<[MissionSnapshot](js-apis-inner-application-missionSnapshot-sys.md)> | 任务快照信息。 | 652 653**示例:** 654 655```ts 656import missionManager from '@ohos.app.ability.missionManager'; 657import { BusinessError } from '@ohos.base'; 658 659let testMissionId = 2; 660try { 661 missionManager.getLowResolutionMissionSnapShot('', testMissionId).then((data: missionManager.MissionSnapshot) => { 662 console.info(`getLowResolutionMissionSnapShot successfully. Data: ${JSON.stringify(data)}`); 663 }).catch((error: BusinessError) => { 664 console.error(`getLowResolutionMissionSnapShot failed. Cause: ${error.message}`); 665 }); 666} catch (error) { 667 let err: BusinessError = error as BusinessError; 668 console.error(`getLowResolutionMissionSnapShot failed. Cause: ${err.message}`); 669} 670``` 671 672 673## missionManager.lockMission 674 675lockMission(missionId: number, callback: AsyncCallback<void>): void 676 677锁定指定任务id的任务,以回调函数的方式返回。 678 679**需要权限**:ohos.permission.MANAGE_MISSIONS 680 681**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 682 683**系统API**: 此接口为系统接口,三方应用不支持调用。 684 685**参数:** 686 687 | 参数名 | 类型 | 必填 | 说明 | 688 | -------- | -------- | -------- | -------- | 689 | missionId | number | 是 | 任务ID。 | 690 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 691 692**错误码**: 693 694| 错误码ID | 错误信息 | 695| ------- | -------- | 696| 16300001 | Mission not found. | 697 698以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 699 700**示例:** 701 702```ts 703import missionManager from '@ohos.app.ability.missionManager'; 704import { BusinessError } from '@ohos.base'; 705 706let testMissionId = 2; 707try { 708 missionManager.lockMission(testMissionId, (err: BusinessError, data: void) => { 709 if (err) { 710 console.error(`lockMission failed: ${err.message}`); 711 } else { 712 console.info(`lockMission successfully: ${JSON.stringify(data)}`); 713 } 714 }); 715} catch (error) { 716 let err: BusinessError = error as BusinessError; 717 console.error(`lockMission failed: ${err.message}`); 718} 719``` 720 721## missionManager.lockMission 722 723lockMission(missionId: number): Promise<void> 724 725锁定指定任务id的任务,以promise方式返回。 726 727**需要权限**:ohos.permission.MANAGE_MISSIONS 728 729**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 730 731**系统API**: 此接口为系统接口,三方应用不支持调用。 732 733**参数:** 734 735 | 参数名 | 类型 | 必填 | 说明 | 736 | -------- | -------- | -------- | -------- | 737 | missionId | number | 是 | 任务ID。 | 738 739**返回值:** 740 741 | 类型 | 说明 | 742 | -------- | -------- | 743 | Promise<void> | promise方式返回执行结果。 | 744 745**错误码**: 746 747| 错误码ID | 错误信息 | 748| ------- | -------- | 749| 16300001 | Mission not found. | 750 751以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 752 753**示例:** 754```ts 755import missionManager from '@ohos.app.ability.missionManager'; 756import { BusinessError } from '@ohos.base'; 757 758let testMissionId = 2; 759try { 760 missionManager.lockMission(testMissionId).then((data: void) => { 761 console.info(`lockMission successfully. Data: ${JSON.stringify(data)}`); 762 }).catch((error: BusinessError) => { 763 console.error(`lockMission failed. Cause: ${error.message}`); 764 }); 765} catch (error) { 766 let err: BusinessError = error as BusinessError; 767 console.error(`lockMission failed. Cause: ${err.message}`); 768} 769``` 770 771## missionManager.unlockMission 772 773unlockMission(missionId: number, callback: AsyncCallback<void>): void 774 775解锁指定任务id的任务,以回调函数的方式返回。 776 777**需要权限**:ohos.permission.MANAGE_MISSIONS 778 779**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 780 781**系统API**: 此接口为系统接口,三方应用不支持调用。 782 783**参数:** 784 785| 参数名 | 类型 | 必填 | 说明 | 786| -------- | -------- | -------- | -------- | 787| missionId | number | 是 | 任务ID。 | 788| callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 789 790**错误码**: 791 792| 错误码ID | 错误信息 | 793| ------- | -------- | 794| 16300001 | Mission not found. | 795 796以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 797 798**示例:** 799```ts 800import missionManager from '@ohos.app.ability.missionManager'; 801import { BusinessError } from '@ohos.base'; 802 803let testMissionId = 2; 804try { 805 missionManager.unlockMission(testMissionId, (err: BusinessError, data: void) => { 806 if (err) { 807 console.error(`unlockMission failed: ${err.message}`); 808 } else { 809 console.info(`unlockMission successfully: ${JSON.stringify(data)}`); 810 } 811 }); 812} catch (error) { 813 let err: BusinessError = error as BusinessError; 814 console.error(`unlockMission failed: ${err.message}`); 815} 816``` 817 818## missionManager.unlockMission 819 820unlockMission(missionId: number): Promise<void> 821 822解锁指定任务id的任务,以promise的方式返回。 823 824**需要权限**:ohos.permission.MANAGE_MISSIONS 825 826**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 827 828**系统API**: 此接口为系统接口,三方应用不支持调用。 829 830**参数:** 831 832 | 参数名 | 类型 | 必填 | 说明 | 833 | -------- | -------- | -------- | -------- | 834 | missionId | number | 是 | 任务ID。 | 835 836**返回值:** 837 838 | 类型 | 说明 | 839 | -------- | -------- | 840 | Promise<void> | promise方式返回执行结果。 | 841 842**错误码**: 843 844| 错误码ID | 错误信息 | 845| ------- | -------- | 846| 16300001 | Mission not found. | 847 848以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 849 850**示例:** 851 852```ts 853import missionManager from '@ohos.app.ability.missionManager'; 854import { BusinessError } from '@ohos.base'; 855 856let testMissionId = 2; 857try { 858 missionManager.unlockMission(testMissionId).then((data: void) => { 859 console.info(`unlockMission successfully. Data: ${JSON.stringify(data)}`); 860 }).catch((error: BusinessError) => { 861 console.error(`unlockMission failed. Cause: ${error.message}`); 862 }); 863} catch (error) { 864 let err: BusinessError = error as BusinessError; 865 console.error(`unlockMission failed. Cause: ${err.message}`); 866} 867``` 868 869## missionManager.clearMission 870 871clearMission(missionId: number, callback: AsyncCallback<void>): void 872 873清理指定任务id的任务,无论该任务是否被锁定,以回调函数的方式返回。 874 875**需要权限**:ohos.permission.MANAGE_MISSIONS 876 877**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 878 879**系统API**: 此接口为系统接口,三方应用不支持调用。 880 881**参数:** 882 883 | 参数名 | 类型 | 必填 | 说明 | 884 | -------- | -------- | -------- | -------- | 885 | missionId | number | 是 | 任务ID。 | 886 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 887 888**示例:** 889 890```ts 891import missionManager from '@ohos.app.ability.missionManager'; 892import { BusinessError } from '@ohos.base'; 893 894let testMissionId = 2; 895try { 896 missionManager.clearMission(testMissionId, (err: BusinessError, data: void) => { 897 if (err) { 898 console.error(`clearMission failed: ${err.message}`); 899 } else { 900 console.info(`clearMission successfully: ${JSON.stringify(data)}`); 901 } 902 }); 903} catch (error) { 904 let err: BusinessError = error as BusinessError; 905 console.error(`clearMission failed: ${err.message}`); 906} 907``` 908 909 910## missionManager.clearMission 911 912clearMission(missionId: number): Promise<void> 913 914清理指定任务id的任务,无论该任务是否被锁定,以promise的方式返回。 915 916**需要权限**:ohos.permission.MANAGE_MISSIONS 917 918**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 919 920**系统API**: 此接口为系统接口,三方应用不支持调用。 921 922**参数:** 923 924 | 参数名 | 类型 | 必填 | 说明 | 925 | -------- | -------- | -------- | -------- | 926 | missionId | number | 是 | 任务ID。 | 927 928**返回值:** 929 930 | 类型 | 说明 | 931 | -------- | -------- | 932 | Promise<void> | promise方式返回执行结果。 | 933 934**示例:** 935 936```ts 937import missionManager from '@ohos.app.ability.missionManager'; 938import { BusinessError } from '@ohos.base'; 939 940let testMissionId = 2; 941try { 942 missionManager.clearMission(testMissionId).then((data: void) => { 943 console.info(`clearMission successfully. Data: ${JSON.stringify(data)}`); 944 }).catch((error: BusinessError) => { 945 console.error(`clearMission failed. Cause: ${error.message}`); 946 }); 947} catch (error) { 948 let err: BusinessError = error as BusinessError; 949 console.error(`clearMission failed. Cause: ${err.message}`); 950} 951``` 952 953## missionManager.clearAllMissions 954 955clearAllMissions(callback: AsyncCallback<void>): void 956 957清理所有未锁定的任务,以回调函数的方式返回。 958 959**需要权限**:ohos.permission.MANAGE_MISSIONS 960 961**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 962 963**系统API**: 此接口为系统接口,三方应用不支持调用。 964 965**参数:** 966 967 | 参数名 | 类型 | 必填 | 说明 | 968 | -------- | -------- | -------- | -------- | 969 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 970 971**示例:** 972 973```ts 974import missionManager from '@ohos.app.ability.missionManager'; 975import { BusinessError } from '@ohos.base'; 976 977try { 978 missionManager.clearAllMissions((err: BusinessError) => { 979 if (err) { 980 console.error(`clearAllMissions failed: ${err.message}`); 981 } else { 982 console.info('clearAllMissions successfully.'); 983 } 984 }); 985} catch (error) { 986 let err: BusinessError = error as BusinessError; 987 console.error(`clearAllMissions failed: ${err.message}`); 988} 989``` 990 991## missionManager.clearAllMissions 992 993clearAllMissions(): Promise<void> 994 995清理所有未锁定的任务,以promise的方式返回。 996 997**需要权限**:ohos.permission.MANAGE_MISSIONS 998 999**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1000 1001**系统API**: 此接口为系统接口,三方应用不支持调用。 1002 1003**返回值:** 1004 1005 | 类型 | 说明 | 1006 | -------- | -------- | 1007 | Promise<void> | promise方式返回执行结果。 | 1008 1009**示例:** 1010 1011```ts 1012import missionManager from '@ohos.app.ability.missionManager'; 1013import { BusinessError } from '@ohos.base'; 1014 1015try { 1016 missionManager.clearAllMissions().then((data: void) => { 1017 console.info(`clearAllMissions successfully. Data: ${JSON.stringify(data)}`); 1018 }).catch((err: BusinessError) => { 1019 console.error(`clearAllMissions failed: ${err.message}`); 1020 }); 1021} catch (error) { 1022 let err: BusinessError = error as BusinessError; 1023 console.error(`clearAllMissions failed: ${err.message}`); 1024} 1025``` 1026 1027## missionManager.moveMissionToFront 1028 1029moveMissionToFront(missionId: number, callback: AsyncCallback<void>): void 1030 1031把指定任务id的任务切到前台,以回调函数的方式返回。 1032 1033**需要权限**:ohos.permission.MANAGE_MISSIONS 1034 1035**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1036 1037**系统API**: 此接口为系统接口,三方应用不支持调用。 1038 1039**参数:** 1040 1041 | 参数名 | 类型 | 必填 | 说明 | 1042 | -------- | -------- | -------- | -------- | 1043 | missionId | number | 是 | 任务ID。 | 1044 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 1045 1046**错误码**: 1047 1048| 错误码ID | 错误信息 | 1049| ------- | -------- | 1050| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1051 1052以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 1053 1054**示例:** 1055 1056```ts 1057import missionManager from '@ohos.app.ability.missionManager'; 1058import { BusinessError } from '@ohos.base'; 1059 1060let testMissionId = 2; 1061try { 1062 missionManager.moveMissionToFront(testMissionId, (err: BusinessError, data: void) => { 1063 if (err) { 1064 console.error(`moveMissionToFront failed: ${err.message}`); 1065 } else { 1066 console.info(`moveMissionToFront successfully: ${JSON.stringify(data)}`); 1067 } 1068 }); 1069} catch (error) { 1070 let err: BusinessError = error as BusinessError; 1071 console.error(`moveMissionToFront failed: ${err.message}`); 1072} 1073``` 1074 1075## missionManager.moveMissionToFront 1076 1077moveMissionToFront(missionId: number, options: StartOptions, callback: AsyncCallback<void>): void 1078 1079把指定任务id的任务切到前台,同时指定任务切换到前台时的启动参数,例如窗口模式、设备ID等,以回调函数的方式返回。 1080 1081**需要权限**:ohos.permission.MANAGE_MISSIONS 1082 1083**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1084 1085**系统API**: 此接口为系统接口,三方应用不支持调用。 1086 1087**参数:** 1088 1089 | 参数名 | 类型 | 必填 | 说明 | 1090 | -------- | -------- | -------- | -------- | 1091 | missionId | number | 是 | 任务ID。 | 1092 | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动参数选项,用于指定任务切到前台时的窗口模式,设备ID等。 | 1093 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 1094 1095**错误码**: 1096 1097| 错误码ID | 错误信息 | 1098| ------- | -------- | 1099| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1100 1101以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 1102 1103**示例:** 1104 1105```ts 1106import missionManager from '@ohos.app.ability.missionManager'; 1107import { BusinessError } from '@ohos.base'; 1108 1109let testMissionId = 2; 1110try { 1111 missionManager.moveMissionToFront(testMissionId, {windowMode : 101}, (err: BusinessError, data: void) => { 1112 if (err) { 1113 console.error(`moveMissionToFront failed: ${err.message}`); 1114 } else { 1115 console.info(`moveMissionToFront successfully: ${JSON.stringify(data)}`); 1116 } 1117 }); 1118} catch (error) { 1119 let err: BusinessError = error as BusinessError; 1120 console.error(`moveMissionToFront failed: ${err.message}`); 1121} 1122``` 1123 1124## missionManager.moveMissionToFront 1125 1126moveMissionToFront(missionId: number, options?: StartOptions): Promise<void> 1127 1128把指定任务id的任务切到前台,同时指定任务切换到前台时的启动参数,例如窗口模式、设备ID等,以promise的方式返回。 1129 1130**需要权限**:ohos.permission.MANAGE_MISSIONS 1131 1132**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1133 1134**系统API**: 此接口为系统接口,三方应用不支持调用。 1135 1136**参数:** 1137 1138 | 参数名 | 类型 | 必填 | 说明 | 1139 | -------- | -------- | -------- | -------- | 1140 | missionId | number | 是 | 任务ID。 | 1141 | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动参数选项,用于指定任务切到前台时的窗口模式,设备ID等。默认为空,表示按照默认启动参数。 | 1142 1143**返回值:** 1144 1145 | 类型 | 说明 | 1146 | -------- | -------- | 1147 | Promise<void> | promise方式返回执行结果。 | 1148 1149**错误码**: 1150 1151| 错误码ID | 错误信息 | 1152| ------- | -------- | 1153| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1154 1155以上错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 1156 1157**示例:** 1158 1159```ts 1160import missionManager from '@ohos.app.ability.missionManager'; 1161import { BusinessError } from '@ohos.base'; 1162 1163let testMissionId = 2; 1164try { 1165 missionManager.moveMissionToFront(testMissionId).then((data: void) => { 1166 console.info(`moveMissionToFront successfully. Data: ${JSON.stringify(data)}`); 1167 }).catch((error: BusinessError) => { 1168 console.error(`moveMissionToFront failed. Cause: ${error.message}`); 1169 }); 1170} catch (error) { 1171 let err: BusinessError = error as BusinessError; 1172 console.error(`moveMissionToFront failed. Cause: ${err.message}`); 1173} 1174``` 1175 1176## missionManager.moveMissionsToForeground<sup>10+</sup> 1177 1178moveMissionsToForeground(missionIds: Array<number>, callback: AsyncCallback<void>): void 1179 1180将指定任务批量切到前台,以回调函数的方式返回。 1181 1182**需要权限**:ohos.permission.MANAGE_MISSIONS 1183 1184**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1185 1186**系统接口**: 此接口为系统接口。 1187 1188**参数:** 1189 1190 | 参数名 | 类型 | 必填 | 说明 | 1191 | -------- | -------- | -------- | -------- | 1192 | missionIds | Array<number> | 是 | 任务ID数组。 | 1193 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 1194 1195**错误码**: 1196 1197以下错误码的详细介绍请参见[元能力子系统错误码](errorcode-ability.md)。 1198 1199| 错误码ID | 错误信息 | 1200| ------- | -------- | 1201| 16000050 | Internal error. | 1202 1203**示例:** 1204 1205```ts 1206import abilityManager from '@ohos.app.ability.abilityManager'; 1207import missionManager from '@ohos.app.ability.missionManager'; 1208import { BusinessError } from '@ohos.base'; 1209 1210try { 1211 missionManager.getMissionInfos("", 10, (error: BusinessError, missionInfos: Array<missionManager.MissionInfo>) => { 1212 if (error.code) { 1213 console.log("getMissionInfos failed, error.code:" + JSON.stringify(error.code)); 1214 return; 1215 } 1216 if (missionInfos.length < 1) { 1217 return; 1218 } 1219 1220 let toShows = new Array<number>(); 1221 for (let missionInfo of missionInfos) { 1222 if (missionInfo.abilityState == abilityManager.AbilityState.BACKGROUND) { 1223 toShows.push(missionInfo.missionId); 1224 } 1225 } 1226 missionManager.moveMissionsToForeground(toShows, (err: BusinessError, data: void) => { 1227 if (err) { 1228 console.error(`moveMissionsToForeground failed: ${err.message}`); 1229 } else { 1230 console.info(`moveMissionsToForeground successfully: ${JSON.stringify(data)}`); 1231 } 1232 }); 1233 }); 1234} catch (paramError) { 1235 let code = (paramError as BusinessError).code; 1236 let message = (paramError as BusinessError).message; 1237 console.error(`error: ${code}, ${message} `); 1238} 1239 1240``` 1241 1242## missionManager.moveMissionsToForeground<sup>10+</sup> 1243 1244moveMissionsToForeground(missionIds: Array<number>, topMission: number, callback: AsyncCallback<void>): void 1245 1246将指定任务批量切换到前台,并将任务ID等于topMission的任务移动到最顶层,以回调函数的方式返回。 1247 1248**需要权限**:ohos.permission.MANAGE_MISSIONS 1249 1250**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1251 1252**系统接口**: 此接口为系统接口。 1253 1254**参数:** 1255 1256 | 参数名 | 类型 | 必填 | 说明 | 1257 | -------- | -------- | -------- | -------- | 1258 | missionIds | Array<number> | 是 | 任务ID数组。 | 1259 | topMission | number | 是 | 待移动到最顶层的任务ID | 1260 | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 | 1261 1262**错误码**: 1263 1264以下错误码的详细介绍请参见[元能力子系统错误码](errorcode-ability.md)。 1265 1266| 错误码ID | 错误信息 | 1267| ------- | -------- | 1268| 16000050 | Internal error. | 1269 1270**示例:** 1271 1272```ts 1273import abilityManager from '@ohos.app.ability.abilityManager'; 1274import missionManager from '@ohos.app.ability.missionManager'; 1275import { BusinessError } from '@ohos.base'; 1276 1277try { 1278 missionManager.getMissionInfos("", 10, (error: BusinessError, missionInfos: Array<missionManager.MissionInfo>) => { 1279 if (error.code) { 1280 console.log("getMissionInfos failed, error.code:" + JSON.stringify(error.code)); 1281 return; 1282 } 1283 if (missionInfos.length < 1) { 1284 return; 1285 } 1286 1287 let toShows = new Array<number>(); 1288 for (let missionInfo of missionInfos) { 1289 if (missionInfo.abilityState == abilityManager.AbilityState.BACKGROUND) { 1290 toShows.push(missionInfo.missionId); 1291 } 1292 } 1293 missionManager.moveMissionsToForeground(toShows, toShows[0], (err: BusinessError, data: void) => { 1294 if (err) { 1295 console.error(`moveMissionsToForeground failed: ${err.message}`); 1296 } else { 1297 console.info(`moveMissionsToForeground successfully: ${JSON.stringify(data)}`); 1298 } 1299 }); 1300 }); 1301} catch (paramError) { 1302 let code = (paramError as BusinessError).code; 1303 let message = (paramError as BusinessError).message; 1304 console.error(`error: ${code}, ${message} `); 1305} 1306 1307``` 1308 1309## missionManager.moveMissionsToForeground<sup>10+</sup> 1310 1311moveMissionsToForeground(missionIds: Array<number>, topMission?: number): Promise<void> 1312 1313将指定任务批量切到前台,并将任务ID等于topMission的任务移动到最顶层,以promise的方式返回。 1314 1315**需要权限**:ohos.permission.MANAGE_MISSIONS 1316 1317**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1318 1319**系统接口**: 此接口为系统接口。 1320 1321**参数:** 1322 1323 | 参数名 | 类型 | 必填 | 说明 | 1324 | -------- | -------- | -------- | -------- | 1325 | missionIds | Array<number> | 是 | 任务ID数组。 | 1326 | topMission | number | 否 | 待移动到最顶层的任务ID。默认值为-1,表示将默认任务移动到最顶层。 | 1327 1328**返回值:** 1329 1330 | 类型 | 说明 | 1331 | -------- | -------- | 1332 | Promise<void> | promise方式返回执行结果。 | 1333 1334**错误码**: 1335 1336以下错误码的详细介绍请参见[元能力子系统错误码](errorcode-ability.md)。 1337 1338| 错误码ID | 错误信息 | 1339| ------- | -------- | 1340| 16000050 | Internal error. | 1341 1342**示例:** 1343 1344```ts 1345import abilityManager from '@ohos.app.ability.abilityManager'; 1346import missionManager from '@ohos.app.ability.missionManager'; 1347import { BusinessError } from '@ohos.base'; 1348 1349try { 1350 missionManager.getMissionInfos("", 10, (error: BusinessError, missionInfos: Array<missionManager.MissionInfo>) => { 1351 if (error.code) { 1352 console.log("getMissionInfos failed, error.code:" + JSON.stringify(error.code)); 1353 return; 1354 } 1355 if (missionInfos.length < 1) { 1356 return; 1357 } 1358 1359 let toShows = new Array<number>(); 1360 for (let missionInfo of missionInfos) { 1361 if (missionInfo.abilityState == abilityManager.AbilityState.BACKGROUND) { 1362 toShows.push(missionInfo.missionId); 1363 } 1364 } 1365 missionManager.moveMissionsToForeground(toShows, toShows[0]).then(() => { 1366 console.log("moveMissionsToForeground is called" ); 1367 }); 1368 }); 1369} catch (paramError) { 1370 let code = (paramError as BusinessError).code; 1371 let message = (paramError as BusinessError).message; 1372 console.error(`error: ${code}, ${message} `); 1373} 1374 1375``` 1376 1377## missionManager.moveMissionsToBackground<sup>10+</sup> 1378 1379moveMissionsToBackground(missionIds: Array<number>, callback: AsyncCallback<Array<number>>): void 1380 1381将指定任务批量切到后台,以回调函数的方式返回, 返回的结果任务ID按被隐藏时的任务层级排序。 1382 1383**需要权限**:ohos.permission.MANAGE_MISSIONS 1384 1385**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1386 1387**系统接口**: 此接口为系统接口。 1388 1389**参数:** 1390 1391 | 参数名 | 类型 | 必填 | 说明 | 1392 | -------- | -------- | -------- | -------- | 1393 | missionIds | Array<number> | 是 | 任务ID数组。 | 1394 | callback | AsyncCallback<Array<number>> | 是 | 执行结果回调函数。 | 1395 1396**错误码**: 1397 1398以下错误码的详细介绍请参见[元能力子系统错误码](errorcode-ability.md)。 1399 1400| 错误码ID | 错误信息 | 1401| ------- | -------- | 1402| 16000050 | Internal error. | 1403 1404**示例:** 1405 1406```ts 1407import abilityManager from '@ohos.app.ability.abilityManager'; 1408import missionManager from '@ohos.app.ability.missionManager'; 1409import { BusinessError } from '@ohos.base'; 1410 1411try { 1412 missionManager.getMissionInfos("", 10, (error: BusinessError, missionInfos: Array<missionManager.MissionInfo>) => { 1413 if (error.code) { 1414 console.log("getMissionInfos failed, error.code:" + JSON.stringify(error.code)); 1415 return; 1416 } 1417 1418 let toHides = new Array<number>(); 1419 for (let missionInfo of missionInfos) { 1420 if (missionInfo.abilityState == abilityManager.AbilityState.FOREGROUND) { 1421 toHides.push(missionInfo.missionId); 1422 } 1423 } 1424 missionManager.moveMissionsToBackground(toHides, (err: BusinessError, data: Array<number>) => { 1425 if (err) { 1426 console.error(`moveMissionsToBackground failed: ${err.message}`); 1427 } else { 1428 console.info(`moveMissionsToBackground successfully: ${JSON.stringify(data)}`); 1429 } 1430 }); 1431 }); 1432} catch (paramError) { 1433 let code = (paramError as BusinessError).code; 1434 let message = (paramError as BusinessError).message; 1435 console.error(`error: ${code}, ${message} `); 1436} 1437``` 1438 1439## missionManager.moveMissionsToBackground<sup>10+</sup> 1440 1441moveMissionsToBackground(missionIds : Array<number>): Promise<Array<number>> 1442 1443将指定任务批量切到后台,以promise的方式返回, 返回的结果按被隐藏时的任务层级排序。 1444 1445**需要权限**:ohos.permission.MANAGE_MISSIONS 1446 1447**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission 1448 1449**系统接口**: 此接口为系统接口。 1450 1451**参数:** 1452 1453 | 参数名 | 类型 | 必填 | 说明 | 1454 | -------- | -------- | -------- | -------- | 1455 | missionIds | Array<number> | 是 | 任务ID数组。 | 1456 1457**返回值:** 1458 1459 | 类型 | 说明 | 1460 | -------- | -------- | 1461 | Promise<Array<number>> | promise方式返回执行结果。 | 1462 1463**错误码**: 1464 1465以下错误码的详细介绍请参见[元能力子系统错误码](errorcode-ability.md)。 1466 1467| 错误码ID | 错误信息 | 1468| ------- | -------- | 1469| 16000050 | Internal error. | 1470 1471**示例:** 1472 1473```ts 1474import abilityManager from '@ohos.app.ability.abilityManager'; 1475import missionManager from '@ohos.app.ability.missionManager'; 1476import { BusinessError } from '@ohos.base'; 1477 1478try { 1479 missionManager.getMissionInfos("", 10, (error: BusinessError, missionInfos: Array<missionManager.MissionInfo>) => { 1480 if (error.code) { 1481 console.log("getMissionInfos failed, error.code:" + JSON.stringify(error.code)); 1482 return; 1483 } 1484 1485 let toHides = new Array<number>(); 1486 for (let missionInfo of missionInfos) { 1487 if (missionInfo.abilityState == abilityManager.AbilityState.FOREGROUND) { 1488 toHides.push(missionInfo.missionId); 1489 } 1490 } 1491 missionManager.moveMissionsToBackground(toHides).then((hideRes: Array<number>) => { 1492 console.log("moveMissionsToBackground is called, res: "+ JSON.stringify(hideRes)); 1493 }); 1494 }); 1495} catch (paramError) { 1496 let code = (paramError as BusinessError).code; 1497 let message = (paramError as BusinessError).message; 1498 console.error(`error: ${code}, ${message} `); 1499} 1500 1501```