提交 99ae3c3e 编写于 作者: Z zhangxin_T

modify description

Signed-off-by: Nzhangxin_T <zhangxin312@huawei.com>
上级 1b2fa25f
...@@ -126,9 +126,9 @@ ohos.permission.KEEP_BACKGROUND_RUNNING ...@@ -126,9 +126,9 @@ ohos.permission.KEEP_BACKGROUND_RUNNING
## 开发步骤 ## 开发步骤
1. 在config.json文件中配置长时任务权限和后台模式类型,其中ability类型为service 1. 新建Api Version 8的工程后,在工程目录中右键选择“new” -> “Ability” -> “Service Ability” 快速创建Service Ability组件。并在config.json文件中配置长时任务权限、后台模式类型,其中Ability类型为“service”
```json ```
"module": { "module": {
"package": "com.example.myapplication", "package": "com.example.myapplication",
...@@ -203,24 +203,33 @@ ohos.permission.KEEP_BACKGROUND_RUNNING ...@@ -203,24 +203,33 @@ ohos.permission.KEEP_BACKGROUND_RUNNING
## 开发实例 ## 开发实例
基于FA的Service Ability使用,参考[ServiceAbility开发指导](../ability/fa-serviceability.md)
当不需要与后台执行的长时任务交互时,可以采用startAbility()方法启动Service Ability。并在Service Ability的onStart回调方法中,调用长时任务的申请接口,声明此服务需要在后台长时运行。当任务执行完,再调用长时任务取消接口,及时释放资源。
当服务启动后,在serviceAbility的onStart回调方法中,调用长时任务的申请接口,声明此服务需要在后台长时运行。在onStop回调方法里,调用长时任务取消接口,声明取消长时任务。 当服务启动后,在serviceAbility的onStart回调方法中,调用长时任务的申请接口,声明此服务需要在后台长时运行。在onStop回调方法里,调用长时任务取消接口,声明取消长时任务。
在service.js文件中: 当需要与后台执行的长时任务交互时(如播放音乐等)。可以采用connectAbility()方法启动并连接Service Ability。在获取到服务的代理对象后,与服务进行通信,控制长时任务的申请和取消。
```js ```js
import backgroundTaskManager from '@ohos.backgroundTaskManager'; import backgroundTaskManager from '@ohos.backgroundTaskManager';
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import wantAgent from '@ohos.wantAgent'; import wantAgent from '@ohos.wantAgent';
import rpc from "@ohos.rpc";
function startBackgroundRunning() { function startBackgroundRunning() {
let wantAgentInfo = { let wantAgentInfo = {
// 点击通知后,将要执行的动作列表
wants: [ wants: [
{ {
bundleName: "com.example.myapplication", bundleName: "com.example.myapplication",
abilityName: "com.example.myapplication.MainAbility" abilityName: "com.example.myapplication.MainAbility"
} }
], ],
// 点击通知后,动作类型
operationType: wantAgent.OperationType.START_ABILITY, operationType: wantAgent.OperationType.START_ABILITY,
// 使用者自定义的一个私有值
requestCode: 0, requestCode: 0,
// 点击通知后,动作执行属性
wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}; };
...@@ -243,10 +252,40 @@ function stopBackgroundRunning() { ...@@ -243,10 +252,40 @@ function stopBackgroundRunning() {
}); });
} }
let mMyStub;
class MyStub extends rpc.RemoteObject {
constructor(des) {
if (typeof des === 'string') {
super(des);
} else {
return null;
}
}
onRemoteRequest(code, data, reply, option) {
console.log('ServiceAbility onRemoteRequest called');
// code 的具体含义用户自定义
if (code === 1) {
// 接收到申请长时任务的请求码
startContinuousTask();
// 此处执行具体长时任务
} else if (code === 2) {
// 接收到取消长时任务的请求码
stopContinuousTask();
} else {
console.log('ServiceAbility unknown request code');
}
return true;
}
}
export default { export default {
onStart(want) { onStart(want) {
console.info('ServiceAbility onStart'); console.info('ServiceAbility onStart');
mMyStub = new MyStub("ServiceAbility-test");
startBackgroundRunning(); startBackgroundRunning();
// 此处执行后台具体的长时任务。
stopBackgroundRunning();
}, },
onStop() { onStop() {
console.info('ServiceAbility onStop'); console.info('ServiceAbility onStop');
...@@ -254,7 +293,7 @@ export default { ...@@ -254,7 +293,7 @@ export default {
}, },
onConnect(want) { onConnect(want) {
console.info('ServiceAbility onConnect'); console.info('ServiceAbility onConnect');
return {}; return mMyStub;
}, },
onReconnect(want) { onReconnect(want) {
console.info('ServiceAbility onReconnect'); console.info('ServiceAbility onReconnect');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册