电子说
任务(Mission)管理相关的基本概念如下:
任务的管理由系统应用(如桌面应用)负责,三方应用无法管理任务。用户通过最近任务列表进行任务的相关交互。当创建任务后,用户可以对最近任务列表进行如下操作:
一个UIAbility实例对应一个单独的任务,因此应用调用[startAbility()
]方法启动一个UIAbility时,就是创建了一个任务。
桌面应用调用[missionManager]的接口管理任务,需要申请ohos.permission.MANAGE_MISSIONS
权限,配置方式请参见[申请应用权限]。
利用missionManager进行任务管理(监听任务变化、获取任务信息、获取任务快照、清理任务、任务加锁/解锁等)。
import missionManager from '@ohos.app.ability.missionManager';
import { BusinessError } from '@ohos.base';
import image from '@ohos.multimedia.image';
import promptAction from '@ohos.promptAction';
import hilog from '@ohos.hilog';
const TAG: string = 'TaskManager';
const DOMAIN_NUMBER: number = 0xFF00;
private listenerId: number = 0;
private missionId: number = 0;
private listener: missionManager.MissionListener = {
// 任务创建
onMissionCreated: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionCreated-------');
},
// 任务销毁
onMissionDestroyed: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionDestroyed-------');
},
// 任务快照变化
onMissionSnapshotChanged: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionMovedToFront-------');
},
// 任务被移动到前台
onMissionMovedToFront: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionClosed-------');
},
// 任务图标变化
onMissionIconUpdated: (mission: number, icon: image.PixelMap) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionIconUpdated-------');
},
// 任务名称变化
onMissionLabelUpdated: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionLabelUpdated-------');
},
// 任务实例被关闭
onMissionClosed: (mission: number) = > {
hilog.info(DOMAIN_NUMBER, TAG, '--------onMissionClosed-------');
}
};
// 1.注册任务变化通知
this.listenerId = missionManager.on('mission', this.listener);
promptAction.showToast({
message: $r('app.string.register_success_toast')
});
hilog.info(DOMAIN_NUMBER, TAG, `missionManager.on success, listenerId = ${this.listenerId}`);
// 2.获取系统最近20个任务
missionManager.getMissionInfos('', 20, (error: BusinessError, missions: Array< missionManager.MissionInfo >) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'getMissionInfos is called, error = ' + JSON.stringify(error));
hilog.info(DOMAIN_NUMBER, TAG, 'size = ' + missions.length);
hilog.info(DOMAIN_NUMBER, TAG, 'missions = ' + JSON.stringify(missions));
// 判断系统最近任务中是否包含etsclock
for (let i = 0;i < missions.length; i++) {
if (missions[i].want.bundleName === 'ohos.samples.etsclock') {
promptAction.showToast({
message: $r('app.string.obtain_success_toast')
});
hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfos.find etsclock, missionId = ${missions[i].missionId}`);
this.missionId = missions[i].missionId;
return;
}
}
promptAction.showToast({
message: $r('app.string.obtain_failed_toast')
});
});
// 3.获取单个任务的详细信息()
missionManager.getMissionInfo('', this.missionId).then((data: missionManager.MissionInfo) = > {
promptAction.showToast({
message: JSON.stringify(data.want.bundleName)
});
hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfo successfully. Data: ${JSON.stringify(data)}`);
}).catch((error: BusinessError) = > {
hilog.info(DOMAIN_NUMBER, TAG, `getMissionInfo failed. Cause: ${error.message}`);
});
// 4.获取任务快照
missionManager.getMissionSnapShot('', this.missionId, (error: BusinessError, snapshot: missionManager.MissionSnapshot) = > {
if (error === null) {
promptAction.showToast({
message: $r('app.string.obtain_snapshot_success_toast')
});
}
hilog.info(DOMAIN_NUMBER, TAG, 'getMissionSnapShot is called, error = ' + JSON.stringify(error));
hilog.info(DOMAIN_NUMBER, TAG, 'bundleName = ' + snapshot.ability.bundleName);
})
// 5.获取低分辨任务快照
missionManager.getLowResolutionMissionSnapShot('', this.missionId, (error: BusinessError, snapshot: missionManager.MissionSnapshot) = > {
if (error === null) {
promptAction.showToast({
message: $r('app.string.obtain_low_snapshot_success_toast')
});
}
hilog.info(DOMAIN_NUMBER, TAG, 'getLowResolutionMissionSnapShot is called, error = ' + JSON.stringify(error));
hilog.info(DOMAIN_NUMBER, TAG, 'bundleName = ' + snapshot.ability.bundleName);
})
// 6-1 加锁任务
missionManager.lockMission(this.missionId).then(() = > {
promptAction.showToast({
message: $r('app.string.lock_success_toast')
});
hilog.info(DOMAIN_NUMBER, TAG, 'lockMission is called ');
});
// 6-2 解锁任务
missionManager.unlockMission(this.missionId).then(() = > {
promptAction.showToast({
message: $r('app.string.unlock_success_toast')
});
hilog.info(DOMAIN_NUMBER, TAG, 'unlockMission is called ');
});
// 7.把任务切到前台
missionManager.moveMissionToFront(this.missionId).then(() = > {
hilog.info(DOMAIN_NUMBER, TAG, 'moveMissionToFront is called ');
});
// 8.删除单个任务
missionManager.clearMission(this.missionId).then(() = > {
promptAction.showToast({
message: $r('app.string.delete_success_toast')
});
hilog.info(DOMAIN_NUMBER, TAG, 'clearMission is called ');
});
// 9.删除全部任务
missionManager.clearAllMissions().catch((err: BusinessError) = > {
hilog.info(DOMAIN_NUMBER, TAG, `${err.code}`);
});
// 10.解注册任务变化通知
missionManager.off('mission', this.listenerId, (error: BusinessError) = > {
if (error === null) {
promptAction.showToast({
message: $r('app.string.unregister_success_toast')
});
}
hilog.info(DOMAIN_NUMBER, TAG, 'unregisterMissionListener');
})
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !