1e41f4b71Sopenharmony_ci# Mission Management Scenarios 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciBefore getting started with the development of mission management, be familiar with the following concepts related to mission management: 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- AbilityRecord: minimum unit for the system service to manage a UIAbility instance. It corresponds to a UIAbility component instance of an application. A maximum of 512 UIAbility instances can be managed on the system service side. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci- MissionRecord: minimum unit for mission management. One MissionRecord has only one AbilityRecord. In other words, a UIAbility component instance corresponds to a mission. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- MissionList: a list of missions started from the home screen. It records the startup relationship between missions. In a MissionList, a mission is started by the mission above it, and the mission at the bottom is started by the home screen. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- MissionListManager: system mission management module that maintains all the MissionLists and is consistent with the list in **Recents**. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci **Figure 1** Mission management 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci  18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciMissions 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**: 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci- Delete a mission. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- Lock or unlock a mission. (Locked missions are not cleared when users attempt to clear all missions in **Recents**.) 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- Clear all missions in **Recents**. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci- Switch a mission to the foreground. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciA UIAbility instance corresponds to an independent mission. Therefore, when an application calls [startAbility()](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to start a UIAbility, a mission is created. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci1. To call [missionManager](../reference/apis-ability-kit/js-apis-application-missionManager-sys.md) to manage missions, the home screen application must request the **ohos.permission.MANAGE_MISSIONS** permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci2. You can use **missionManager** to manage missions, for example, listening for mission changes, obtaining mission information or snapshots, and clearing, locking, or unlocking missions. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci ```ts 39e41f4b71Sopenharmony_ci import { missionManager } from '@kit.AbilityKit'; 40e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 41e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 42e41f4b71Sopenharmony_ci import { promptAction } from '@kit.ArkUI'; 43e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci const TAG: string = 'TaskManager'; 46e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 47e41f4b71Sopenharmony_ci ``` 48e41f4b71Sopenharmony_ci ```ts 49e41f4b71Sopenharmony_ci private listenerId: number = 0; 50e41f4b71Sopenharmony_ci private missionId: number = 0; 51e41f4b71Sopenharmony_ci private listener: missionManager.MissionListener = { 52e41f4b71Sopenharmony_ci // Listen for mission creation. 53e41f4b71Sopenharmony_ci onMissionCreated: (mission: number) => { 54e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionCreated-------'); 55e41f4b71Sopenharmony_ci }, 56e41f4b71Sopenharmony_ci // Listen for mission destruction. 57e41f4b71Sopenharmony_ci onMissionDestroyed: (mission: number) => { 58e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionDestroyed-------'); 59e41f4b71Sopenharmony_ci }, 60e41f4b71Sopenharmony_ci // Listen for mission snapshot changes. 61e41f4b71Sopenharmony_ci onMissionSnapshotChanged: (mission: number) => { 62e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionMovedToFront-------'); 63e41f4b71Sopenharmony_ci }, 64e41f4b71Sopenharmony_ci // Listen for switching the mission to the foreground. 65e41f4b71Sopenharmony_ci onMissionMovedToFront: (mission: number) => { 66e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionClosed-------'); 67e41f4b71Sopenharmony_ci }, 68e41f4b71Sopenharmony_ci // Listen for mission icon changes. 69e41f4b71Sopenharmony_ci onMissionIconUpdated: (mission: number, icon: image.PixelMap) => { 70e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionIconUpdated-------'); 71e41f4b71Sopenharmony_ci }, 72e41f4b71Sopenharmony_ci // Listen for mission name changes. 73e41f4b71Sopenharmony_ci onMissionLabelUpdated: (mission: number) => { 74e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionLabelUpdated-------'); 75e41f4b71Sopenharmony_ci }, 76e41f4b71Sopenharmony_ci // Listen for mission closure events. 77e41f4b71Sopenharmony_ci onMissionClosed: (mission: number) => { 78e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionClosed-------'); 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci }; 81e41f4b71Sopenharmony_ci ``` 82e41f4b71Sopenharmony_ci ```ts 83e41f4b71Sopenharmony_ci // 1. Register a mission change listener. 84e41f4b71Sopenharmony_ci this.listenerId = missionManager.on('mission', this.listener); 85e41f4b71Sopenharmony_ci promptAction.showToast({ 86e41f4b71Sopenharmony_ci message: 'register_success_toast' 87e41f4b71Sopenharmony_ci }); 88e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `missionManager.on success, listenerId = ${this.listenerId}`); 89e41f4b71Sopenharmony_ci ``` 90e41f4b71Sopenharmony_ci ```ts 91e41f4b71Sopenharmony_ci // 2. Obtain the latest 20 missions in the system. 92e41f4b71Sopenharmony_ci missionManager.getMissionInfos('', 20, (error: BusinessError, missions: Array<missionManager.MissionInfo>) => { 93e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'getMissionInfos is called, error = ' + JSON.stringify(error)); 94e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'size = ' + missions.length); 95e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'missions = ' + JSON.stringify(missions)); 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci //Check whether Recents in the system contains etsclock. 98e41f4b71Sopenharmony_ci for (let i = 0;i < missions.length; i++) { 99e41f4b71Sopenharmony_ci if (missions[i].want.bundleName === 'ohos.samples.etsclock') { 100e41f4b71Sopenharmony_ci promptAction.showToast({ 101e41f4b71Sopenharmony_ci message: 'obtain_success_toast' 102e41f4b71Sopenharmony_ci }); 103e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfos.find etsclock, missionId = ${missions[i].missionId}`); 104e41f4b71Sopenharmony_ci this.missionId = missions[i].missionId; 105e41f4b71Sopenharmony_ci return; 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci } 108e41f4b71Sopenharmony_ci promptAction.showToast({ 109e41f4b71Sopenharmony_ci message: 'obtain_failed_toast' 110e41f4b71Sopenharmony_ci }); 111e41f4b71Sopenharmony_ci }); 112e41f4b71Sopenharmony_ci ``` 113e41f4b71Sopenharmony_ci ```ts 114e41f4b71Sopenharmony_ci // 3. Obtain the detailed information about a mission. 115e41f4b71Sopenharmony_ci missionManager.getMissionInfo('', this.missionId).then((data: missionManager.MissionInfo) => { 116e41f4b71Sopenharmony_ci promptAction.showToast({ 117e41f4b71Sopenharmony_ci message: JSON.stringify(data.want.bundleName) 118e41f4b71Sopenharmony_ci }); 119e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfo successfully. Data: ${JSON.stringify(data)}`); 120e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 121e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfo failed. Cause: ${error.message}`); 122e41f4b71Sopenharmony_ci }); 123e41f4b71Sopenharmony_ci ``` 124e41f4b71Sopenharmony_ci ```ts 125e41f4b71Sopenharmony_ci // 4. Obtain the mission snapshot. 126e41f4b71Sopenharmony_ci missionManager.getMissionSnapShot('', this.missionId, (error: BusinessError, snapshot: missionManager.MissionSnapshot) => { 127e41f4b71Sopenharmony_ci if (error === null) { 128e41f4b71Sopenharmony_ci promptAction.showToast({ 129e41f4b71Sopenharmony_ci message: 'obtain_snapshot_success_toast' 130e41f4b71Sopenharmony_ci }); 131e41f4b71Sopenharmony_ci } 132e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'getMissionSnapShot is called, error = ' + JSON.stringify(error)); 133e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'bundleName = ' + snapshot.ability.bundleName); 134e41f4b71Sopenharmony_ci }) 135e41f4b71Sopenharmony_ci ``` 136e41f4b71Sopenharmony_ci ```ts 137e41f4b71Sopenharmony_ci // 5. Obtain the low-resolution mission snapshot. 138e41f4b71Sopenharmony_ci missionManager.getLowResolutionMissionSnapShot('', this.missionId, (error: BusinessError, snapshot: missionManager.MissionSnapshot) => { 139e41f4b71Sopenharmony_ci if (error === null) { 140e41f4b71Sopenharmony_ci promptAction.showToast({ 141e41f4b71Sopenharmony_ci message: 'obtain_low_snapshot_success_toast' 142e41f4b71Sopenharmony_ci }); 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'getLowResolutionMissionSnapShot is called, error = ' + JSON.stringify(error)); 145e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'bundleName = ' + snapshot.ability.bundleName); 146e41f4b71Sopenharmony_ci }) 147e41f4b71Sopenharmony_ci ``` 148e41f4b71Sopenharmony_ci ```ts 149e41f4b71Sopenharmony_ci // 6-1. Lock the mission. 150e41f4b71Sopenharmony_ci missionManager.lockMission(this.missionId).then(() => { 151e41f4b71Sopenharmony_ci promptAction.showToast({ 152e41f4b71Sopenharmony_ci message: 'lock_success_toast' 153e41f4b71Sopenharmony_ci }); 154e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'lockMission is called '); 155e41f4b71Sopenharmony_ci }); 156e41f4b71Sopenharmony_ci ``` 157e41f4b71Sopenharmony_ci ```ts 158e41f4b71Sopenharmony_ci // 6-2. Unlock the mission. 159e41f4b71Sopenharmony_ci missionManager.unlockMission(this.missionId).then(() => { 160e41f4b71Sopenharmony_ci promptAction.showToast({ 161e41f4b71Sopenharmony_ci message: 'unlock_success_toast' 162e41f4b71Sopenharmony_ci }); 163e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'unlockMission is called '); 164e41f4b71Sopenharmony_ci }); 165e41f4b71Sopenharmony_ci ``` 166e41f4b71Sopenharmony_ci ```ts 167e41f4b71Sopenharmony_ci // 7. Switch the mission to the foreground. 168e41f4b71Sopenharmony_ci missionManager.moveMissionToFront(this.missionId).then(() => { 169e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'moveMissionToFront is called '); 170e41f4b71Sopenharmony_ci }); 171e41f4b71Sopenharmony_ci ``` 172e41f4b71Sopenharmony_ci ```ts 173e41f4b71Sopenharmony_ci // 8. Clear a single mission. 174e41f4b71Sopenharmony_ci missionManager.clearMission(this.missionId).then(() => { 175e41f4b71Sopenharmony_ci promptAction.showToast({ 176e41f4b71Sopenharmony_ci message: 'delete_success_toast' 177e41f4b71Sopenharmony_ci }); 178e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'clearMission is called '); 179e41f4b71Sopenharmony_ci }); 180e41f4b71Sopenharmony_ci ``` 181e41f4b71Sopenharmony_ci ```ts 182e41f4b71Sopenharmony_ci // 9. Clear all missions. 183e41f4b71Sopenharmony_ci missionManager.clearAllMissions().catch((err: BusinessError) => { 184e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `${err.code}`); 185e41f4b71Sopenharmony_ci }); 186e41f4b71Sopenharmony_ci ``` 187e41f4b71Sopenharmony_ci ```ts 188e41f4b71Sopenharmony_ci // 10. Deregister the mission change listener. 189e41f4b71Sopenharmony_ci missionManager.off('mission', this.listenerId, (error: BusinessError) => { 190e41f4b71Sopenharmony_ci if (error === null) { 191e41f4b71Sopenharmony_ci promptAction.showToast({ 192e41f4b71Sopenharmony_ci message: 'unregister_success_toast' 193e41f4b71Sopenharmony_ci }); 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, 'unregisterMissionListener'); 196e41f4b71Sopenharmony_ci }) 197e41f4b71Sopenharmony_ci ``` 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci 200