fileio longtime task

Signed-off-by: anaiping <anaiping1@huawei.com>
Change-Id: I8ba32d824dfed2141875068c40d4461a724c3341
This commit is contained in:
anaiping 2023-07-04 21:10:18 +08:00
parent 1f33b77f8e
commit 5a733b6c62
4 changed files with 55 additions and 1 deletions

View File

@ -22,6 +22,9 @@
},
{
"name": "ohos.permission.GET_BUNDLE_INFO"
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
},
{
"name": "ohos.permission.MANAGE_LOCAL_ACCOUNTS"
@ -68,6 +71,11 @@
"launchType": "standard"
},
{
"backgroundModes": [
"dataTransfer",
"location",
"multiDeviceConnection"
],
"srcPath": "ServiceAbility",
"name": ".ServiceAbility",
"icon": "$media:icon",
@ -93,4 +101,4 @@
}
]
}
}
}

View File

@ -15,13 +15,59 @@
import rpc from "@ohos.rpc";
import fileio from '@ohos.fileio';
import backgroundTaskManager from '@ohos.backgroundTaskManager';
import wantAgent from '@ohos.wantAgent';
import featureAbility from '@ohos.ability.featureAbility';
function startContinuousTask() {
let wantAgentInfo = {
wants: [
{
bundleName: "com.ohos.fileiotest",
abilityName: "com.ohos.fileiotest.ServiceAbility"
}
],
operationType: wantAgent.OperationType.START_SERVICE,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
};
wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
try{
backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
backgroundTaskManager.BackgroundMode.MULTI_DEVICE_CONNECTION, wantAgentObj).then(() => {
console.info("Operation startBackgroundRunning succeeded");
}).catch((err) => {
console.error("Operation startBackgroundRunning failed Cause: " + err);
});
}catch(error){
console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
});
}
function stopContinuousTask() {
try{
backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
console.info("Operation stopBackgroundRunning succeeded");
}).catch((err) => {
console.error("Operation stopBackgroundRunning failed Cause: " + err);
});
}catch(error){
console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
}
}
export default {
onStart() {
console.info('FileioServer: onStart')
startContinuousTask();
console.info('FileioServer: startContinuousTask');
},
onStop() {
console.info('FileioServer: onStop')
stopContinuousTask();
console.info('FileioServer: stopContinuousTask');
},
onCommand(want, startId) {
console.info('FileioServer: onCommand, want: ' + JSON.stringify(want) + ', startId: ' + startId)