• 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  ![mission-list-manager](figures/mission-list-manager.png)
17
18
19Missions 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**:
20
21
22- Delete a mission.
23
24- Lock or unlock a mission. (Locked missions are not cleared when users attempt to clear all missions in **Recents**.)
25
26- Clear all missions in **Recents**.
27
28- Switch a mission to the foreground.
29
30
31A UIAbility instance corresponds to an independent mission. Therefore, when an application calls the **startAbility()** method to start a UIAbility, a mission is created.
32
33
34To 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 [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
35
36
37You 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:
38
39```ts
40import missionManager from '@ohos.app.ability.missionManager'
41
42let listener = {
43    // Listen for mission creation.
44    onMissionCreated: function (mission) {
45        console.info("--------onMissionCreated-------")
46    },
47    // Listen for mission destruction.
48    onMissionDestroyed: function (mission) {
49        console.info("--------onMissionDestroyed-------")
50    },
51    // Listen for mission snapshot changes.
52    onMissionSnapshotChanged: function (mission) {
53        console.info("--------onMissionSnapshotChanged-------")
54    },
55    // Listen for switching the mission to the foreground.
56    onMissionMovedToFront: function (mission) {
57        console.info("--------onMissionMovedToFront-------")
58    },
59    // Listen for mission icon changes.
60    onMissionIconUpdated: function (mission, icon) {
61        console.info("--------onMissionIconUpdated-------")
62    },
63    // Listen for mission name changes.
64    onMissionLabelUpdated: function (mission) {
65        console.info("--------onMissionLabelUpdated-------")
66    },
67    // Listen for mission closure events.
68    onMissionClosed: function (mission) {
69        console.info("--------onMissionClosed-------")
70    }
71};
72
73// 1. Register a mission change listener.
74let listenerId = missionManager.on('mission', listener);
75
76// 2. Obtain the latest 20 missions in the system.
77missionManager.getMissionInfos("", 20, (error, missions) => {
78    console.info("getMissionInfos is called, error.code = " + error.code);
79    console.info("size = " + missions.length);
80    console.info("missions = " + JSON.stringify(missions));
81});
82
83// 3. Obtain the detailed information about a mission.
84let missionId = 11; // The mission ID 11 is only an example.
85let mission = missionManager.getMissionInfo("", missionId).catch(function (err) {
86    console.info(err);
87});
88
89// 4. Obtain the mission snapshot.
90missionManager.getMissionSnapShot("", missionId, (error, snapshot) => {
91    console.info("getMissionSnapShot is called, error.code = " + error.code);
92    console.info("bundleName = " + snapshot.ability.bundleName);
93})
94
95// 5. Obtain the low-resolution mission snapshot.
96missionManager.getLowResolutionMissionSnapShot("", missionId, (error, snapshot) => {
97    console.info("getLowResolutionMissionSnapShot is called, error.code = " + error.code);
98    console.info("bundleName = " + snapshot.ability.bundleName);
99})
100
101// 6. Lock or unlock the mission.
102missionManager.lockMission(missionId).then(() => {
103    console.info("lockMission is called ");
104});
105
106missionManager.unlockMission(missionId).then(() => {
107    console.info("unlockMission is called ");
108});
109
110// 7. Switch the mission to the foreground.
111missionManager.moveMissionToFront(missionId).then(() => {
112    console.info("moveMissionToFront is called ");
113});
114
115// 8. Clear a single mission.
116missionManager.clearMission(missionId).then(() => {
117    console.info("clearMission is called ");
118});
119
120// 9. Clear all missions.
121missionManager.clearAllMissions().catch(function (err) {
122    console.info(err);
123});
124
125// 10. Deregister the mission change listener.
126missionManager.off('mission', listenerId, (error) => {
127    console.info("unregisterMissionListener");
128})
129```
130