• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.missionManager (missionManager)
2
3missionManager模块提供系统任务管理能力,包括对系统任务执行锁定、解锁、清理、切换到前台等操作。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import missionManager from '@ohos.app.ability.missionManager';
13```
14
15## 权限列表
16
17ohos.permission.MANAGE_MISSIONS
18
19## missionManager.on
20
21on(type:'mission', listener: MissionListener): number;
22
23注册系统任务状态监听器。
24
25**需要权限**:ohos.permission.MANAGE_MISSIONS
26
27**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
28
29**系统API**: 此接口为系统接口,三方应用不支持调用。
30
31**参数:**
32
33  | 参数名 | 类型 | 必填 | 说明 |
34  | -------- | -------- | -------- | -------- |
35  | listener | [MissionListener](js-apis-inner-application-missionListener.md) | 是 | 系统任务监听器。 |
36
37**返回值:**
38
39  | 类型 | 说明 |
40  | -------- | -------- |
41  | number | 监听器的index值,由系统创建,在注册系统任务状态监听时分配,和监听器一一对应 。 |
42
43**示例:**
44
45```ts
46import missionManager from '@ohos.app.ability.missionManager';
47import UIAbility from '@ohos.app.ability.UIAbility';
48
49let listener = {
50    onMissionCreated: function (mission) {console.log('--------onMissionCreated-------');},
51    onMissionDestroyed: function (mission) {console.log('--------onMissionDestroyed-------');},
52    onMissionSnapshotChanged: function (mission) {console.log('--------onMissionSnapshotChanged-------');},
53    onMissionMovedToFront: function (mission) {console.log('--------onMissionMovedToFront-------');},
54    onMissionIconUpdated: function (mission, icon) {console.log('--------onMissionIconUpdated-------');},
55    onMissionClosed: function (mission) {console.log('--------onMissionClosed-------');},
56    onMissionLabelUpdated: function (mission) {console.log('--------onMissionLabelUpdated-------');}
57};
58
59let listenerId = -1;
60
61export default class EntryAbility extends UIAbility {
62    onCreate(want, launchParam) {
63        console.log('[Demo] EntryAbility onCreate');
64        globalThis.abilityWant = want;
65        globalThis.context = this.context;
66    }
67
68    onDestroy() {
69        try {
70            if (listenerId !== -1) {
71                missionManager.off('mission', listenerId).catch(function (err) {
72                    console.log(err);
73                });
74            }
75        } catch (paramError) {
76            console.error('error: ${paramError.code}, ${paramError.message}');
77        }
78        console.log('[Demo] EntryAbility onDestroy');
79    }
80
81    onWindowStageCreate(windowStage) {
82        // Main window is created, set main page for this ability
83        console.log('[Demo] EntryAbility onWindowStageCreate');
84        try {
85            listenerId = missionManager.on('mission', listener);
86        } catch (paramError) {
87            console.error('error: ${paramError.code}, ${paramError.message}');
88        }
89
90        windowStage.loadContent('pages/index', (err, data) => {
91            if (err.code) {
92                console.error('Failed to load the content. Cause: ${JSON.stringify(err)}');
93                return;
94            }
95            console.info('Succeeded in loading the content. Data: ${JSON.stringify(data)}');
96        });
97
98        if (globalThis.flag) {
99            return;
100        }
101    }
102};
103```
104
105
106## missionManager.off
107
108off(type: 'mission', listenerId: number, callback: AsyncCallback<void>): void;
109
110解注册任务状态监听器。
111
112**需要权限**:ohos.permission.MANAGE_MISSIONS
113
114**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
115
116**系统API**: 此接口为系统接口,三方应用不支持调用。
117
118**参数:**
119
120  | 参数名 | 类型 | 必填 | 说明 |
121  | -------- | -------- | -------- | -------- |
122  | listenerId | number | 是 | 系统任务状态监器法的index值,和监听器一一对应,由on方法返回。 |
123  | callback | AsyncCallback<void> | 是 | 执行结果回调函数。 |
124
125**错误码**:
126
127| 错误码ID | 错误信息 |
128| ------- | -------- |
129| 16300002 | Input error. The specified mission listener does not exist. |
130
131以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
132
133**示例:**
134
135```ts
136import missionManager from '@ohos.app.ability.missionManager';
137import UIAbility from '@ohos.app.ability.UIAbility';
138
139let listener = {
140    onMissionCreated: function (mission) {console.log('--------onMissionCreated-------');},
141    onMissionDestroyed: function (mission) {console.log('--------onMissionDestroyed-------');},
142    onMissionSnapshotChanged: function (mission) {console.log('--------onMissionSnapshotChanged-------');},
143    onMissionMovedToFront: function (mission) {console.log('--------onMissionMovedToFront-------');},
144    onMissionIconUpdated: function (mission, icon) {console.log('--------onMissionIconUpdated-------');},
145    onMissionClosed: function (mission) {console.log('--------onMissionClosed-------');},
146    onMissionLabelUpdated: function (mission) {console.log('--------onMissionLabelUpdated-------');}
147};
148
149let listenerId = -1;
150
151export default class EntryAbility extends UIAbility {
152    onCreate(want, launchParam) {
153        console.log('[Demo] EntryAbility onCreate');
154        globalThis.abilityWant = want;
155        globalThis.context = this.context;
156    }
157
158    onDestroy() {
159        try {
160            if (listenerId !== -1) {
161                missionManager.off('mission', listenerId, (err) => {
162                    console.log(err);
163                });
164            }
165        } catch (paramError) {
166            console.error('error: ${paramError.code}, ${paramError.message}');
167        }
168        console.log('[Demo] EntryAbility onDestroy');
169    }
170
171    onWindowStageCreate(windowStage) {
172        // Main window is created, set main page for this ability
173        console.log('[Demo] EntryAbility onWindowStageCreate');
174        try {
175            listenerId = missionManager.on('mission', listener);
176        } catch (paramError) {
177            console.error('error: ${paramError.code}, ${paramError.message}');
178        }
179
180        windowStage.loadContent('pages/index', (err, data) => {
181            if (err.code) {
182                console.error('Failed to load the content. Cause: ${JSON.stringify(err)}');
183                return;
184            }
185            console.info('Succeeded in loading the content. Data: ${JSON.stringify(data)}');
186        });
187
188        if (globalThis.flag) {
189            return;
190        }
191    }
192};
193```
194
195
196## missionManager.off
197
198off(type: 'mission', listenerId: number): Promise<void>;
199
200解注册任务状态监听,以promise方式返回执行结果。
201
202**需要权限**:ohos.permission.MANAGE_MISSIONS
203
204**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
205
206**系统API**: 此接口为系统接口,三方应用不支持调用。
207
208**参数:**
209
210  | 参数名 | 类型 | 必填 | 说明 |
211  | -------- | -------- | -------- | -------- |
212  | listenerId | number | 是 | 系统任务状态监听器的index值,和监听器一一对应,由on方法返回。 |
213
214**返回值:**
215
216  | 类型 | 说明 |
217  | -------- | -------- |
218  | Promise<void> | promise方式返回执行结果。 |
219
220**错误码**:
221
222| 错误码ID | 错误信息 |
223| ------- | -------- |
224| 16300002 | Input error. The specified mission listener does not exist. |
225
226以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
227
228**示例:**
229
230```ts
231import missionManager from '@ohos.app.ability.missionManager';
232import UIAbility from '@ohos.app.ability.UIAbility';
233
234let listener = {
235    onMissionCreated: function (mission) {console.log('--------onMissionCreated-------');},
236    onMissionDestroyed: function (mission) {console.log('--------onMissionDestroyed-------');},
237    onMissionSnapshotChanged: function (mission) {console.log('--------onMissionSnapshotChanged-------');},
238    onMissionMovedToFront: function (mission) {console.log('--------onMissionMovedToFront-------');},
239    onMissionIconUpdated: function (mission, icon) {console.log('--------onMissionIconUpdated-------');},
240    onMissionClosed: function (mission) {console.log('--------onMissionClosed-------');},
241    onMissionLabelUpdated: function (mission) {console.log('--------onMissionLabelUpdated-------');}
242};
243
244let listenerId = -1;
245
246export default class EntryAbility extends UIAbility {
247    onCreate(want, launchParam) {
248        console.log('[Demo] EntryAbility onCreate');
249        globalThis.abilityWant = want;
250        globalThis.context = this.context;
251    }
252
253    onDestroy() {
254        try {
255            if (listenerId !== -1) {
256                missionManager.off('mission', listenerId).catch(function (err) {
257                    console.log(err);
258                });
259            }
260        } catch (paramError) {
261            console.error('error: ${paramError.code}, ${paramError.message}');
262        }
263        console.log('[Demo] EntryAbility onDestroy');
264    }
265
266    onWindowStageCreate(windowStage) {
267        // Main window is created, set main page for this ability
268        console.log('[Demo] EntryAbility onWindowStageCreate');
269        try {
270            listenerId = missionManager.on('mission', listener);
271        } catch (paramError) {
272            console.error('error: ${paramError.code}, ${paramError.message}');
273        }
274
275        windowStage.loadContent('pages/index', (err, data) => {
276            if (err.code) {
277                console.error('Failed to load the content. Cause: ${JSON.stringify(err)}');
278                return;
279            }
280            console.info('Succeeded in loading the content. Data: ${JSON.stringify(data)}');
281        });
282
283        if (globalThis.flag) {
284            return;
285        }
286    }
287};
288```
289
290
291## missionManager.getMissionInfo
292
293getMissionInfo(deviceId: string, missionId: number, callback: AsyncCallback<MissionInfo>): void;
294
295获取任务信息,以异步回调的方式返回任务信息。
296
297**需要权限**:ohos.permission.MANAGE_MISSIONS
298
299**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
300
301**系统API**: 此接口为系统接口,三方应用不支持调用。
302
303**参数:**
304
305  | 参数名 | 类型 | 必填 | 说明 |
306  | -------- | -------- | -------- | -------- |
307  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
308  | missionId | number | 是 | 任务ID。 |
309  | callback | AsyncCallback<[MissionInfo](./js-apis-inner-application-missionInfo.md)> | 是 | 执行结果回调函数,返回任务信息。 |
310
311**示例:**
312
313  ```ts
314    import missionManager from '@ohos.app.ability.missionManager';
315
316    let testMissionId = 1;
317
318    missionManager.getMissionInfos('',10)
319    .then((allMissions) => {
320        try {
321        if (allMissions && allMissions.length > 0) {
322            testMissionId = allMissions[0].missionId;
323        }
324
325        missionManager.getMissionInfo('', testMissionId, (error, mission) => {
326            if (error) {
327            console.error('getMissionInfo failed, error.code: ${error.code}, error.message: ${error.message}');
328            } else {
329            console.log('mission.missionId = ${mission.missionId}');
330            console.log('mission.runningState = ${mission.runningState}');
331            console.log('mission.lockedState = ${mission.lockedState}');
332            console.log('mission.timestamp = ${mission.timestamp}');
333            console.log('mission.label = ${mission.label}');
334            console.log('mission.iconPath = ${mission.iconPath}');
335            }
336        });
337        } catch (paramError) {
338        console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
339        }
340    })
341    .catch(function(err){console.log(err);});
342  ```
343
344## missionManager.getMissionInfo
345
346getMissionInfo(deviceId: string, missionId: number): Promise<MissionInfo>;
347
348获取任务信息,以promise方式返回任务信息。
349
350**需要权限**:ohos.permission.MANAGE_MISSIONS
351
352**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
353
354**系统API**: 此接口为系统接口,三方应用不支持调用。
355
356**参数:**
357
358  | 参数名 | 类型 | 必填 | 说明 |
359  | -------- | -------- | -------- | -------- |
360  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
361  | missionId | number | 是 | 任务ID。 |
362
363**返回值:**
364
365  | 类型 | 说明 |
366  | -------- | -------- |
367  | Promise<[MissionInfo](./js-apis-inner-application-missionInfo.md)> | 任务信息。 |
368
369**示例:**
370
371```ts
372import missionManager from '@ohos.app.ability.missionManager';
373
374let testMissionId = 1;
375try {
376    missionManager.getMissionInfo('', testMissionId).then((data) => {
377        console.info('getMissionInfo successfully. Data: ${JSON.stringify(data)}');
378    }).catch(error => {
379        console.error('getMissionInfo failed. Cause: ${error.message}');
380    });
381} catch (error) {
382    console.error('getMissionInfo failed. Cause: ${error.message}');
383}
384```
385
386## missionManager.getMissionInfos
387
388getMissionInfos(deviceId: string, numMax: number, callback: AsyncCallback<Array<MissionInfo>>): void;
389
390获取所有任务信息,以回调函数的方式返回任务信息数组。
391
392**需要权限**:ohos.permission.MANAGE_MISSIONS
393
394**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
395
396**系统API**: 此接口为系统接口,三方应用不支持调用。
397
398**参数:**
399
400  | 参数名 | 类型 | 必填 | 说明 |
401  | -------- | -------- | -------- | -------- |
402  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
403  | numMax | number | 是 | 任务信息数量上限。 |
404  | callback | AsyncCallback<Array<[MissionInfo](./js-apis-inner-application-missionInfo.md)>> | 是 | 执行结果回调函数,返回任务信息数组。 |
405
406**示例:**
407
408  ```ts
409  import missionManager from '@ohos.app.ability.missionManager';
410
411  try {
412    missionManager.getMissionInfos('', 10, (error, missions) => {
413      if (error) {
414          console.error('getMissionInfos failed, error.code: ${error.code}, error.message: ${error.message}');
415      } else {
416        console.log('size = ${missions.length}');
417        console.log('missions = ${JSON.stringify(missions)}');
418      }
419    });
420  } catch (paramError) {
421    console.error('error: ${paramError.code}, ${paramError.message}');
422  }
423  ```
424
425
426## missionManager.getMissionInfos
427
428getMissionInfos(deviceId: string, numMax: number): Promise<Array<MissionInfo>>;
429
430获取所有任务信息,以promise的方式返回任务信息数组。
431
432**需要权限**:ohos.permission.MANAGE_MISSIONS
433
434**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
435
436**系统API**: 此接口为系统接口,三方应用不支持调用。
437
438**参数:**
439
440  | 参数名 | 类型 | 必填 | 说明 |
441  | -------- | -------- | -------- | -------- |
442  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
443  | numMax | number | 是 | 任务信息数量上限。 |
444
445**返回值:**
446
447  | 类型 | 说明 |
448  | -------- | -------- |
449  | Promise<Array<[MissionInfo](./js-apis-inner-application-missionInfo.md)>> | 任务信息数组。 |
450
451**示例:**
452
453```ts
454import missionManager from '@ohos.app.ability.missionManager';
455
456try {
457    missionManager.getMissionInfos('', 10).then((data) => {
458        console.info('getMissionInfos successfully. Data: ${JSON.stringify(data)}');
459    }).catch(error => {
460        console.error('getMissionInfos failed. Cause: ${error.message}');
461    });
462} catch (error) {
463    console.error('getMissionInfos failed. Cause: ${error.message}');
464}
465```
466
467## missionManager.getMissionSnapShot
468
469getMissionSnapShot(deviceId: string, missionId: number, callback: AsyncCallback<MissionSnapshot>): void;
470
471获取任务快照,以回调函数的方式返回快照内容。
472
473**需要权限**:ohos.permission.MANAGE_MISSIONS
474
475**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
476
477**系统API**: 此接口为系统接口,三方应用不支持调用。
478
479**参数:**
480
481  | 参数名 | 类型 | 必填 | 说明 |
482  | -------- | -------- | -------- | -------- |
483  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
484  | missionId | number | 是 | 任务ID。 |
485  | callback | AsyncCallback<[MissionSnapshot](js-apis-inner-application-missionSnapshot.md)> | 是 | 执行结果回调函数,返回任务快照信息。 |
486
487**示例:**
488```ts
489import missionManager from '@ohos.app.ability.missionManager';
490
491let testMissionId = 2;
492try {
493    missionManager.getMissionSnapShot('', testMissionId, (err, data) => {
494        if (err) {
495            console.error('getMissionSnapShot failed: ${err.message}');
496        } else {
497            console.info('getMissionSnapShot successfully: ${JSON.stringify(data)}');
498        }
499    });
500} catch (err) {
501    console.error('getMissionSnapShot failed: ${err.message}');
502}
503```
504
505## missionManager.getMissionSnapShot
506
507getMissionSnapShot(deviceId: string, missionId: number): Promise<MissionSnapshot>;
508
509获取任务快照,以promise的方式返回快照内容。
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
524**返回值:**
525
526  | 类型 | 说明 |
527  | -------- | -------- |
528  | Promise<[MissionSnapshot](js-apis-inner-application-missionSnapshot.md)> | 任务快照信息。 |
529
530**示例:**
531```ts
532import missionManager from '@ohos.app.ability.missionManager';
533
534let testMissionId = 2;
535try {
536    missionManager.getMissionSnapShot('', testMissionId).then((data) => {
537        console.info('getMissionSnapShot successfully. Data: ${JSON.stringify(data)}');
538    }).catch(error => {
539        console.error('getMissionSnapShot failed. Cause: ${error.message}');
540    });
541} catch (error) {
542    console.error('getMissionSnapShot failed. Cause: ${error.message}');
543}
544```
545
546## missionManager.getLowResolutionMissionSnapShot
547
548getLowResolutionMissionSnapShot(deviceId: string, missionId: number, callback: AsyncCallback\<MissionSnapshot>): void;
549
550获取任务低分辨率快照。
551
552**需要权限**:ohos.permission.MANAGE_MISSIONS
553
554**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
555
556**系统API**: 此接口为系统接口,三方应用不支持调用。
557
558**参数:**
559
560  | 参数名 | 类型 | 必填 | 说明 |
561  | -------- | -------- | -------- | -------- |
562  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
563  | missionId | number | 是 | 任务ID。 |
564  | callback | AsyncCallback&lt;[MissionSnapshot](js-apis-inner-application-missionSnapshot.md)&gt; | 是 | 执行结果回调函数,返回任务快照信息。 |
565
566**示例:**
567```ts
568import missionManager from '@ohos.app.ability.missionManager';
569
570let testMissionId = 2;
571try {
572    missionManager.getLowResolutionMissionSnapShot('', testMissionId, (err, data) => {
573        if (err) {
574            console.error('getLowResolutionMissionSnapShot failed: ${err.message}');
575        } else {
576            console.info('getLowResolutionMissionSnapShot successfully: ${JSON.stringify(data)}');
577        }
578    });
579} catch (err) {
580    console.error('getLowResolutionMissionSnapShot failed: ${err.message}');
581}
582```
583
584## missionManager.getLowResolutionMissionSnapShot
585
586getLowResolutionMissionSnapShot(deviceId: string, missionId: number): Promise\<MissionSnapshot>;
587
588获取任务低分辨率快照。
589
590**需要权限**:ohos.permission.MANAGE_MISSIONS
591
592**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
593
594**系统API**: 此接口为系统接口,三方应用不支持调用。
595
596**参数:**
597
598  | 参数名 | 类型 | 必填 | 说明 |
599  | -------- | -------- | -------- | -------- |
600  | deviceId | string | 是 | 设备ID,本机默认为空字符串。 |
601  | missionId | number | 是 | 任务ID。 |
602
603**返回值:**
604
605  | 类型 | 说明 |
606  | -------- | -------- |
607  | Promise&lt;[MissionSnapshot](js-apis-inner-application-missionSnapshot.md)&gt; | 任务快照信息。 |
608
609**示例:**
610
611```ts
612import missionManager from '@ohos.app.ability.missionManager';
613
614let testMissionId = 2;
615try {
616    missionManager.getLowResolutionMissionSnapShot('', testMissionId).then((data) => {
617        console.info('getLowResolutionMissionSnapShot successfully. Data: ${JSON.stringify(data)}');
618    }).catch(error => {
619        console.error('getLowResolutionMissionSnapShot failed. Cause: ${error.message}');
620    });
621} catch (error) {
622    console.error('getLowResolutionMissionSnapShot failed. Cause: ${error.message}');
623}
624```
625
626
627## missionManager.lockMission
628
629lockMission(missionId: number, callback: AsyncCallback&lt;void&gt;): void;
630
631锁定指定任务id的任务,以回调函数的方式返回。
632
633**需要权限**:ohos.permission.MANAGE_MISSIONS
634
635**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
636
637**系统API**: 此接口为系统接口,三方应用不支持调用。
638
639**参数:**
640
641  | 参数名 | 类型 | 必填 | 说明 |
642  | -------- | -------- | -------- | -------- |
643  | missionId | number | 是 | 任务ID。 |
644  | callback | AsyncCallback&lt;void&gt; | 是 | 执行结果回调函数。 |
645
646**错误码**:
647
648| 错误码ID | 错误信息 |
649| ------- | -------- |
650| 16300001 | Mission not found. |
651
652以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
653
654**示例:**
655
656```ts
657import missionManager from '@ohos.app.ability.missionManager';
658
659let testMissionId = 2;
660try {
661    missionManager.lockMission(testMissionId, (err, data) => {
662        if (err) {
663            console.error('lockMission failed: ${err.message}');
664        } else {
665            console.info('lockMission successfully: ${JSON.stringify(data)}');
666        }
667    });
668} catch (err) {
669    console.error('lockMission failed: ${err.message}');
670}
671```
672
673## missionManager.lockMission
674
675lockMission(missionId: number): Promise&lt;void&gt;;
676
677锁定指定任务id的任务,以promise方式返回。
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
691**返回值:**
692
693  | 类型 | 说明 |
694  | -------- | -------- |
695  | Promise&lt;void&gt; | promise方式返回执行结果。 |
696
697**错误码**:
698
699| 错误码ID | 错误信息 |
700| ------- | -------- |
701| 16300001 | Mission not found. |
702
703以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
704
705**示例:**
706```ts
707import missionManager from '@ohos.app.ability.missionManager';
708
709let testMissionId = 2;
710try {
711    missionManager.lockMission(testMissionId).then((data) => {
712        console.info('lockMission successfully. Data: ${JSON.stringify(data)}');
713    }).catch(error => {
714        console.error('lockMission failed. Cause: ${error.message}');
715    });
716} catch (error) {
717    console.error('lockMission failed. Cause: ${error.message}');
718}
719```
720
721## missionManager.unlockMission
722
723unlockMission(missionId: number, callback: AsyncCallback&lt;void&gt;): void;
724
725解锁指定任务id的任务,以回调函数的方式返回。
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| callback | AsyncCallback&lt;void&gt; | 是 | 执行结果回调函数。 |
739
740**错误码**:
741
742| 错误码ID | 错误信息 |
743| ------- | -------- |
744| 16300001 | Mission not found. |
745
746以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
747
748**示例:**
749```ts
750import missionManager from '@ohos.app.ability.missionManager';
751
752let testMissionId = 2;
753try {
754    missionManager.unlockMission(testMissionId, (err, data) => {
755        if (err) {
756            console.error('unlockMission failed: ${err.message}');
757        } else {
758            console.info('unlockMission successfully: ${JSON.stringify(data)}');
759        }
760    });
761} catch (err) {
762    console.error('unlockMission failed: ${err.message}');
763}
764```
765
766## missionManager.unlockMission
767
768unlockMission(missionId: number): Promise&lt;void&gt;;
769
770解锁指定任务id的任务,以promise的方式返回。
771
772**需要权限**:ohos.permission.MANAGE_MISSIONS
773
774**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
775
776**系统API**: 此接口为系统接口,三方应用不支持调用。
777
778**参数:**
779
780  | 参数名 | 类型 | 必填 | 说明 |
781  | -------- | -------- | -------- | -------- |
782  | missionId | number | 是 | 任务ID。 |
783
784**返回值:**
785
786  | 类型 | 说明 |
787  | -------- | -------- |
788  | Promise&lt;void&gt; | promise方式返回执行结果。 |
789
790**错误码**:
791
792| 错误码ID | 错误信息 |
793| ------- | -------- |
794| 16300001 | Mission not found. |
795
796以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
797
798**示例:**
799
800```ts
801import missionManager from '@ohos.app.ability.missionManager';
802
803let testMissionId = 2;
804try {
805    missionManager.unlockMission(testMissionId).then((data) => {
806        console.info('unlockMission successfully. Data: ${JSON.stringify(data)}');
807    }).catch(error => {
808        console.error('unlockMission failed. Cause: ${error.message}');
809    });
810} catch (error) {
811    console.error('unlockMission failed. Cause: ${error.message}');
812}
813```
814
815## missionManager.clearMission
816
817clearMission(missionId: number, callback: AsyncCallback&lt;void&gt;): void;
818
819清理指定任务id的任务,无论该任务是否被锁定,以回调函数的方式返回。
820
821**需要权限**:ohos.permission.MANAGE_MISSIONS
822
823**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
824
825**系统API**: 此接口为系统接口,三方应用不支持调用。
826
827**参数:**
828
829  | 参数名 | 类型 | 必填 | 说明 |
830  | -------- | -------- | -------- | -------- |
831  | missionId | number | 是 | 任务ID。 |
832  | callback | AsyncCallback&lt;void&gt; | 是 | 执行结果回调函数。 |
833
834**示例:**
835
836```ts
837import missionManager from '@ohos.app.ability.missionManager';
838
839let testMissionId = 2;
840try {
841    missionManager.clearMission(testMissionId, (err, data) => {
842        if (err) {
843            console.error('clearMission failed: ${err.message}');
844        } else {
845            console.info('clearMission successfully: ${JSON.stringify(data)}');
846        }
847    });
848} catch (err) {
849    console.error('clearMission failed: ${err.message}');
850}
851```
852
853
854## missionManager.clearMission
855
856clearMission(missionId: number): Promise&lt;void&gt;;
857
858清理指定任务id的任务,无论该任务是否被锁定,以promise的方式返回。
859
860**需要权限**:ohos.permission.MANAGE_MISSIONS
861
862**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
863
864**系统API**: 此接口为系统接口,三方应用不支持调用。
865
866**参数:**
867
868  | 参数名 | 类型 | 必填 | 说明 |
869  | -------- | -------- | -------- | -------- |
870  | missionId | number | 是 | 任务ID。 |
871
872**返回值:**
873
874  | 类型 | 说明 |
875  | -------- | -------- |
876  | Promise&lt;void&gt; | promise方式返回执行结果。 |
877
878**示例:**
879
880```ts
881import missionManager from '@ohos.app.ability.missionManager';
882
883let testMissionId = 2;
884try {
885    missionManager.clearMission(testMissionId).then((data) => {
886        console.info('clearMission successfully. Data: ${JSON.stringify(data)}');
887    }).catch(error => {
888        console.error('clearMission failed. Cause: ${error.message}');
889    });
890} catch (error) {
891    console.error('clearMission failed. Cause: ${error.message}');
892}
893```
894
895## missionManager.clearAllMissions
896
897clearAllMissions(callback: AsyncCallback&lt;void&gt;): void;
898
899清理所有未锁定的任务,以回调函数的方式返回。
900
901**需要权限**:ohos.permission.MANAGE_MISSIONS
902
903**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
904
905**系统API**: 此接口为系统接口,三方应用不支持调用。
906
907**示例:**
908
909```ts
910import missionManager from '@ohos.app.ability.missionManager';
911
912try {
913    missionManager.clearAllMissions(err => {
914        if (err) {
915            console.error('clearAllMissions failed: ${err.message}');
916        } else {
917            console.info('clearAllMissions successfully.');
918        }
919    });
920} catch (err) {
921    console.error('clearAllMissions failed: ${err.message}');
922}
923```
924
925## missionManager.clearAllMissions
926
927clearAllMissions(): Promise&lt;void&gt;;
928
929清理所有未锁定的任务,以promise的方式返回。
930
931**需要权限**:ohos.permission.MANAGE_MISSIONS
932
933**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
934
935**系统API**: 此接口为系统接口,三方应用不支持调用。
936
937**返回值:**
938
939  | 类型 | 说明 |
940  | -------- | -------- |
941  | Promise&lt;void&gt; | promise方式返回执行结果。 |
942
943**示例:**
944
945```ts
946import missionManager from '@ohos.app.ability.missionManager';
947
948try {
949    missionManager.clearAllMissions(bundleName).then((data) => {
950        console.info('clearAllMissions successfully. Data: ${JSON.stringify(data)}');
951    }).catch(err => {
952        console.error('clearAllMissions failed: ${err.message}');
953    });
954} catch (err) {
955    console.error('clearAllMissions failed: ${err.message}');
956}
957```
958
959## missionManager.moveMissionToFront
960
961moveMissionToFront(missionId: number, callback: AsyncCallback&lt;void&gt;): void;
962
963把指定任务id的任务切到前台,以回调函数的方式返回。
964
965**需要权限**:ohos.permission.MANAGE_MISSIONS
966
967**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
968
969**系统API**: 此接口为系统接口,三方应用不支持调用。
970
971**参数:**
972
973  | 参数名 | 类型 | 必填 | 说明 |
974  | -------- | -------- | -------- | -------- |
975  | missionId | number | 是 | 任务ID。 |
976  | callback | AsyncCallback&lt;void&gt; | 是 | 执行结果回调函数。 |
977
978**错误码**:
979
980| 错误码ID | 错误信息 |
981| ------- | -------- |
982| 16000009 | An ability cannot be started or stopped in Wukong mode. |
983
984以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
985
986**示例:**
987
988```ts
989import missionManager from '@ohos.app.ability.missionManager';
990
991let testMissionId = 2;
992try {
993    missionManager.moveMissionToFront(testMissionId, (err, data) => {
994        if (err) {
995            console.error('moveMissionToFront failed: ${err.message}');
996        } else {
997            console.info('moveMissionToFront successfully: ${JSON.stringify(data)}');
998        }
999    });
1000} catch (err) {
1001    console.error('moveMissionToFront failed: ${err.message}');
1002}
1003```
1004
1005## missionManager.moveMissionToFront
1006
1007moveMissionToFront(missionId: number, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void;
1008
1009把指定任务id的任务切到前台,同时指定任务切换到前台时的启动参数,例如窗口模式、设备ID等,以回调函数的方式返回。
1010
1011**需要权限**:ohos.permission.MANAGE_MISSIONS
1012
1013**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
1014
1015**系统API**: 此接口为系统接口,三方应用不支持调用。
1016
1017**参数:**
1018
1019  | 参数名 | 类型 | 必填 | 说明 |
1020  | -------- | -------- | -------- | -------- |
1021  | missionId | number | 是 | 任务ID。 |
1022  | options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动参数选项,用于指定任务切到前台时的窗口模式,设备ID等。 |
1023  | callback | AsyncCallback&lt;void&gt; | 是 | 执行结果回调函数。 |
1024
1025**错误码**:
1026
1027| 错误码ID | 错误信息 |
1028| ------- | -------- |
1029| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1030
1031以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
1032
1033**示例:**
1034
1035```ts
1036import missionManager from '@ohos.app.ability.missionManager';
1037
1038let testMissionId = 2;
1039try {
1040    missionManager.moveMissionToFront(testMissionId, {windowMode : 101}, (err, data) => {
1041        if (err) {
1042            console.error('moveMissionToFront failed: ${err.message}');
1043        } else {
1044            console.info('moveMissionToFront successfully: ${JSON.stringify(data)}');
1045        }
1046    });
1047} catch (err) {
1048    console.error('moveMissionToFront failed: ${err.message}');
1049}
1050```
1051
1052## missionManager.moveMissionToFront
1053
1054moveMissionToFront(missionId: number, options?: StartOptions): Promise&lt;void&gt;;
1055
1056把指定任务id的任务切到前台,同时指定任务切换到前台时的启动参数,例如窗口模式、设备ID等,以promise的方式返回。
1057
1058**需要权限**:ohos.permission.MANAGE_MISSIONS
1059
1060**系统能力**:SystemCapability.Ability.AbilityRuntime.Mission
1061
1062**系统API**: 此接口为系统接口,三方应用不支持调用。
1063
1064**参数:**
1065
1066  | 参数名 | 类型 | 必填 | 说明 |
1067  | -------- | -------- | -------- | -------- |
1068  | missionId | number | 是 | 任务ID。 |
1069  | options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动参数选项,用于指定任务切到前台时的窗口模式,设备ID等。 |
1070
1071**返回值:**
1072
1073  | 类型 | 说明 |
1074  | -------- | -------- |
1075  | Promise&lt;void&gt; | promise方式返回执行结果。 |
1076
1077**错误码**:
1078
1079| 错误码ID | 错误信息 |
1080| ------- | -------- |
1081| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1082
1083以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
1084
1085**示例:**
1086
1087```ts
1088import missionManager from '@ohos.app.ability.missionManager';
1089
1090let testMissionId = 2;
1091try {
1092    missionManager.moveMissionToFront(testMissionId).then((data) => {
1093        console.info('moveMissionToFront successfully. Data: ${JSON.stringify(data)}');
1094    }).catch(error => {
1095        console.error('moveMissionToFront failed. Cause: ${error.message}');
1096    });
1097} catch (error) {
1098    console.error('moveMissionToFront failed. Cause: ${error.message}');
1099}
1100```
1101