提交 e70629ea 编写于 作者: W wusongqing

update docs against 7699

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 e78e7fb7
...@@ -30,7 +30,7 @@ A Service ability is used to run tasks in the background, such as playing music ...@@ -30,7 +30,7 @@ A Service ability is used to run tasks in the background, such as playing music
}, },
onConnect(want) { onConnect(want) {
console.log('ServiceAbility OnConnect'); console.log('ServiceAbility OnConnect');
return null; return new FirstServiceAbilityStub('test');
}, },
onDisconnect(want) { onDisconnect(want) {
console.log('ServiceAbility OnDisConnect'); console.log('ServiceAbility OnDisConnect');
...@@ -113,8 +113,6 @@ let promise = featureAbility.startAbility( ...@@ -113,8 +113,6 @@ let promise = featureAbility.startAbility(
Once created, the Service ability keeps running in the background. The system does not stop or destroy it unless memory resources must be reclaimed. Once created, the Service ability keeps running in the background. The system does not stop or destroy it unless memory resources must be reclaimed.
### Connecting to a Local Service Ability ### Connecting to a Local Service Ability
If you need to connect a Service ability to a Page ability or to a Service ability in another application, you must first implement the **IAbilityConnection** API for the connection. A Service ability allows other abilities to connect to it through **connectAbility()**. If you need to connect a Service ability to a Page ability or to a Service ability in another application, you must first implement the **IAbilityConnection** API for the connection. A Service ability allows other abilities to connect to it through **connectAbility()**.
...@@ -124,7 +122,7 @@ You can use either of the following methods to connect to a Service ability: ...@@ -124,7 +122,7 @@ You can use either of the following methods to connect to a Service ability:
1. Using the IDL to automatically generate code 1. Using the IDL to automatically generate code
Use OpenHarmony Interface Definition Language (IDL) to automatically generate the corresponding client, server, and **IRemoteObject** code. For details, see [“Development Using TS” in OpenHarmony IDL Specifications and User Guide](https://gitee.com/openharmony/docs/blob/master/en/application-dev/IDL/idl-guidelines.md#development-using-ts). Use OpenHarmony Interface Definition Language (IDL) to automatically generate the corresponding client, server, and **IRemoteObject** code. For details, see “Development Using TS" in [OpenHarmony IDL Specifications and User Guide](../IDL/idl-guidelines.md).
2. Writing code in the corresponding file 2. Writing code in the corresponding file
...@@ -134,42 +132,37 @@ You can use either of the following methods to connect to a Service ability: ...@@ -134,42 +132,37 @@ You can use either of the following methods to connect to a Service ability:
```javascript ```javascript
import prompt from '@system.prompt' import prompt from '@system.prompt'
let mRemote; var option = {
function onConnectCallback(element, remote){ onConnect: function onConnectCallback(element, proxy) {
console.log('onConnectLocalService onConnectDone element: ' + element); console.log(`onConnectLocalService onConnectDone`)
console.log('onConnectLocalService onConnectDone remote: ' + remote); if (proxy === null) {
mRemote = remote; prompt.showToast({
if (mRemote == null) { message: "Connect service failed"
prompt.showToast({ })
message: "onConnectLocalService not connected yet" return
}); }
return; let data = rpc.MessageParcel.create()
} let reply = rpc.MessageParcel.create()
let option = new rpc.MessageOption(); let option = new rpc.MessageOption()
let data = new rpc.MessageParcel(); data.writeInterfaceToken("connect.test.token")
let reply = new rpc.MessageParcel(); proxy.sendRequest(0, data, reply, option)
data.writeInt(1);
data.writeInt(99);
mRemote.sendRequest(1, data, reply, option).then((result) => {
console.log('sendRequest success');
let msg = reply.readInt();
prompt.showToast({ prompt.showToast({
message: "onConnectLocalService connect result: " + msg, message: "Connect service success"
duration: 3000 })
}); },
}).catch((e) => { onDisconnect: function onDisconnectCallback(element) {
console.log('sendRequest error:' + e); console.log(`onConnectLocalService onDisconnectDone element:${element}`)
}); prompt.showToast({
message: "Disconnect service success"
} })
},
function onDisconnectCallback(element){ onFailed: function onFailedCallback(code) {
console.log('ConnectAbility onDisconnect Callback') console.log(`onConnectLocalService onFailed errCode:${code}`)
} prompt.showToast({
message: "Connect local service onFailed"
function onFailedCallback(code){ })
console.log('ConnectAbility onFailed Callback') }
} }
``` ```
...@@ -196,45 +189,28 @@ You can use either of the following methods to connect to a Service ability: ...@@ -196,45 +189,28 @@ You can use either of the following methods to connect to a Service ability:
```javascript ```javascript
import rpc from "@ohos.rpc"; import rpc from "@ohos.rpc";
let mMyStub; class FirstServiceAbilityStub extends rpc.RemoteObject {
export default { constructor(des: any) {
onStart() { if (typeof des === 'string') {
class MyStub extends rpc.RemoteObject{ super(des)
constructor(des) { } else {
if (typeof des === 'string') { return
super(des); }
} }
return null;
} onRemoteRequest(code: number, data: any, reply: any, option: any) {
onRemoteRequest(code, data, reply, option) { console.log(printLog + ` onRemoteRequest called`)
console.log("ServiceAbility onRemoteRequest called"); if (code === 1) {
if (code === 1) { let string = data.readString()
let op1 = data.readInt(); console.log(printLog + ` string=${string}`)
let op2 = data.readInt(); let result = Array.from(string).sort().join('')
console.log("op1 = " + op1 + ", op2 = " + op2); console.log(printLog + ` result=${result}`)
reply.writeInt(op1 + op2); reply.writeString(result)
} else { } else {
console.log("ServiceAbility unknown request code"); console.log(printLog + ` unknown request code`)
} }
return true; return true;
}
}
mMyStub = new MyStub("ServiceAbility-test");
},
onCommand(want, startId) {
console.log('ServiceAbility onCommand');
},
onConnect(want) {
console.log('ServiceAbility OnConnect');
return mMyStub;
},
onDisconnect(want) {
console.log('ServiceAbility OnDisConnect');
},
onStop() {
console.log('ServiceAbility onStop');
},
} }
``` ```
...@@ -253,40 +229,36 @@ The following code snippet shows how to implement the callbacks: ...@@ -253,40 +229,36 @@ The following code snippet shows how to implement the callbacks:
```ts ```ts
import prompt from '@system.prompt' import prompt from '@system.prompt'
let mRemote; var option = {
function onConnectCallback(element, remote){ onConnect: function onConnectCallback(element, proxy) {
console.log('onConnectRemoteService onConnectDone element: ' + element); console.log(`onConnectRemoteService onConnectDone`)
console.log('onConnectRemoteService onConnectDone remote: ' + remote); if (proxy === null) {
mRemote = remote; prompt.showToast({
if (mRemote == null) { message: "Connect service failed"
prompt.showToast({ })
message: "onConnectRemoteService not connected yet" return
}); }
return; let data = rpc.MessageParcel.create()
} let reply = rpc.MessageParcel.create()
let option = new rpc.MessageOption(); let option = new rpc.MessageOption()
let data = new rpc.MessageParcel(); data.writeInterfaceToken("connect.test.token")
let reply = new rpc.MessageParcel(); proxy.sendRequest(0, data, reply, option)
data.writeInt(1);
data.writeInt(99);
mRemote.sendRequest(1, data, reply, option).then((result) => {
console.log('sendRequest success');
let msg = reply.readInt();
prompt.showToast({ prompt.showToast({
message: "onConnectRemoteService connect result: " + msg, message: "Connect service success"
duration: 3000 })
}); },
}).catch((e) => { onDisconnect: function onDisconnectCallback(element) {
console.log('sendRequest error:' + e); console.log(`onConnectRemoteService onDisconnectDone element:${element}`)
}); prompt.showToast({
} message: "Disconnect service success"
})
function onDisconnectCallback(element){ },
console.log('ConnectRemoteAbility onDisconnect Callback') onFailed: function onFailedCallback(code) {
} console.log(`onConnectRemoteService onFailed errCode:${code}`)
prompt.showToast({
function onFailedCallback(code){ message: "Connect local service onFailed"
console.log('ConnectRemoteAbility onFailed Callback') })
}
} }
``` ```
...@@ -372,23 +344,25 @@ The following code snippet shows how the Service ability instance returns itself ...@@ -372,23 +344,25 @@ The following code snippet shows how the Service ability instance returns itself
```ts ```ts
import rpc from "@ohos.rpc"; import rpc from "@ohos.rpc";
class FirstServiceAbilityStub extends rpc.RemoteObject{ class FirstServiceAbilityStub extends rpc.RemoteObject {
constructor(des) { constructor(des: any) {
if (typeof des === 'string') { if (typeof des === 'string') {
super(des); super(des)
} else { } else {
return null; return
} }
} }
onRemoteRequest(code, data, reply, option) {
console.log("ServiceAbility onRemoteRequest called"); onRemoteRequest(code: number, data: any, reply: any, option: any) {
console.log(printLog + ` onRemoteRequest called`)
if (code === 1) { if (code === 1) {
let op1 = data.readInt(); let string = data.readString()
let op2 = data.readInt(); console.log(printLog + ` string=${string}`)
console.log("op1 = " + op1 + ", op2 = " + op2); let result = Array.from(string).sort().join('')
reply.writeInt(op1 + op2); console.log(printLog + ` result=${result}`)
reply.writeString(result)
} else { } else {
console.log("ServiceAbility unknown request code"); console.log(printLog + ` unknown request code`)
} }
return true; return true;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册