• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Mission Management Scenarios
2
3
4Before getting started with the development of mission management, be familiar with the following concepts related to mission management:
5
6
7- AbilityRecord: minimum unit for the system service to manage a UIAbility instance. It corresponds to a UIAbility component instance of an application.
8
9- MissionRecord: minimum unit for mission management. One MissionRecord has only one AbilityRecord. In other words, a UIAbility component instance corresponds to a mission.
10
11- MissionList: a list of missions started from the home screen. It records the startup relationship between missions. In a MissionList, an above mission is started by the mission under it, and the mission at the bottom is started by the home screen.
12
13- MissionListManager: system mission management module that maintains all the MissionLists and is consistent with the list in **Recents**.
14
15  **Figure 1** Mission management
16
17  ![mission-list-manager](figures/mission-list-manager.png)
18
19
20Missions are managed by system applications (such as home screen), rather than third-party applications. Users interact with missions through **Recents**. After creating a mission, users can perform the following operations on **Recents**:
21
22
23- Delete a mission.
24
25- Lock or unlock a mission. (Locked missions are not cleared when users attempt to clear all missions in **Recents**.)
26
27- Clear all missions in **Recents**.
28
29- Switch a mission to the foreground.
30
31
32A UIAbility instance corresponds to an independent mission. Therefore, when an application calls **startAbility()** to start a UIAbility, a mission is created.
33
34
35To call [missionManager](../reference/apis/js-apis-application-missionManager.md) to manage missions, the home screen application must request the **ohos.permission.MANAGE_MISSIONS** permission. For details about the configuration, see [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
36
37
38You can use **missionManager** to manage missions, for example, listening for mission changes, obtaining mission information or snapshots, and clearing, locking, or unlocking missions. The sample code is as follows:
39
40```ts
41import missionManager from '@ohos.app.ability.missionManager'
42
43let listener = {
44    // Listen for mission creation.
45    onMissionCreated: function (mission) {
46        console.info("--------onMissionCreated-------")
47    },
48    // Listen for mission destruction.
49    onMissionDestroyed: function (mission) {
50        console.info("--------onMissionDestroyed-------")
51    },
52    // Listen for mission snapshot changes.
53    onMissionSnapshotChanged: function (mission) {
54        console.info("--------onMissionSnapshotChanged-------")
55    },
56    // Listen for switching the mission to the foreground.
57    onMissionMovedToFront: function (mission) {
58        console.info("--------onMissionMovedToFront-------")
59    },
60    // Listen for mission icon changes.
61    onMissionIconUpdated: function (mission, icon) {
62        console.info("--------onMissionIconUpdated-------")
63    },
64    // Listen for mission name changes.
65    onMissionLabelUpdated: function (mission) {
66        console.info("--------onMissionLabelUpdated-------")
67    },
68    // Listen for mission closure events.
69    onMissionClosed: function (mission) {
70        console.info("--------onMissionClosed-------")
71    }
72};
73
74// 1. Register a mission change listener.
75let listenerId = missionManager.on('mission', listener);
76
77// 2. Obtain the latest 20 missions in the system.
78missionManager.getMissionInfos("", 20, (error, missions) => {
79    console.info("getMissionInfos is called, error.code = " + error.code);
80    console.info("size = " + missions.length);
81    console.info("missions = " + JSON.stringify(missions));
82});
83
84// 3. Obtain the detailed information about a mission.
85let missionId = 11; // The mission ID 11 is only an example.
86let mission = missionManager.getMissionInfo("", missionId).catch(function (err) {
87    console.info(err);
88});
89
90// 4. Obtain the mission snapshot.
91missionManager.getMissionSnapShot("", missionId, (error, snapshot) => {
92    console.info("getMissionSnapShot is called, error.code = " + error.code);
93    console.info("bundleName = " + snapshot.ability.bundleName);
94})
95
96// 5. Obtain the low-resolution mission snapshot.
97missionManager.getLowResolutionMissionSnapShot("", missionId, (error, snapshot) => {
98    console.info("getLowResolutionMissionSnapShot is called, error.code = " + error.code);
99    console.info("bundleName = " + snapshot.ability.bundleName);
100})
101
102// 6. Lock or unlock the mission.
103missionManager.lockMission(missionId).then(() => {
104    console.info("lockMission is called ");
105});
106
107missionManager.unlockMission(missionId).then(() => {
108    console.info("unlockMission is called ");
109});
110
111// 7. Switch the mission to the foreground.
112missionManager.moveMissionToFront(missionId).then(() => {
113    console.info("moveMissionToFront is called ");
114});
115
116// 8. Clear a single mission.
117missionManager.clearMission(missionId).then(() => {
118    console.info("clearMission is called ");
119});
120
121// 9. Clear all missions.
122missionManager.clearAllMissions().catch(function (err) {
123    console.info(err);
124});
125
126// 10. Deregister the mission change listener.
127missionManager.off('mission', listenerId, (error) => {
128    console.info("unregisterMissionListener");
129})
130```
131