• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Connecting to a ServiceAbility
2
3
4If a ServiceAbility wants to interact with a PageAbility or a ServiceAbility in another application, you must first create a connection by calling **connectAbility()**. This method is defined in the **featureAbility** class for the PageAbility and in the **particleAbility** class for the ServiceAbility. For details about the connection rules, see [Component Startup Rules](component-startup-rules.md). When calling **connectAbility()**, you should pass in a **Want** object containing information about the target ServiceAbility and an **IAbilityConnection** object. **IAbilityConnection** provides the following APIs that you need to implement.
5
6**Table 1** IAbilityConnection APIs
7
8| API| Description|
9| -------- | -------- |
10| onConnect() | Callback invoked when the ServiceAbility is connected.|
11| onDisconnect() | Callback invoked when the ServiceAbility is disconnected.|
12| onFailed() | Callback invoked when the connection to the ServiceAbility fails.|
13
14
15The following sample code enables the PageAbility to create connection callback instances and connect to the local ServiceAbility:
16
17```ts
18import rpc from "@ohos.rpc"
19import prompt from '@system.prompt'
20import featureAbility from '@ohos.ability.featureAbility'
21
22let option = {
23  onConnect: function onConnectCallback(element, proxy) {
24    console.info(`onConnectLocalService onConnectDone`)
25    if (proxy === null) {
26      prompt.showToast({
27        message: "Connect service failed"
28      })
29      return
30    }
31    let data = rpc.MessageParcel.create()
32    let reply = rpc.MessageParcel.create()
33    let option = new rpc.MessageOption()
34    data.writeInterfaceToken("connect.test.token")
35    proxy.sendRequest(0, data, reply, option)
36    prompt.showToast({
37      message: "Connect service success"
38    })
39  },
40  onDisconnect: function onDisconnectCallback(element) {
41    console.info(`onConnectLocalService onDisconnectDone element:${element}`)
42    prompt.showToast({
43      message: "Disconnect service success"
44    })
45  },
46  onFailed: function onFailedCallback(code) {
47    console.info(`onConnectLocalService onFailed errCode:${code}`)
48    prompt.showToast({
49      message: "Connect local service onFailed"
50    })
51  }
52}
53
54let request = {
55  bundleName: "com.example.myapplication",
56  abilityName: "com.example.myapplication.ServiceAbility",
57}
58let connId = featureAbility.connectAbility(request, option)
59```
60
61
62When the ServiceAbility is connected, the **onConnect()** callback is invoked and returns an **IRemoteObject** defining the proxy used for communicating with the ServiceAbility. OpenHarmony provides a default implementation of **IRemoteObject**. You can extend **rpc.RemoteObject** to implement your own class of **IRemoteObject**.
63
64
65The following sample code shows how the ServiceAbility returns itself to the caller:
66
67```ts
68import rpc from "@ohos.rpc"
69
70class FirstServiceAbilityStub extends rpc.RemoteObject {
71  constructor(des: any) {
72    if (typeof des === 'string') {
73      super(des)
74    } else {
75      return
76    }
77  }
78
79  onRemoteRequest(code: number, data: any, reply: any, option: any) {
80    console.info(`onRemoteRequest called`)
81    if (code === 1) {
82      let string = data.readString()
83      console.info(`string=${string}`)
84      let result = Array.from(string).sort().join('')
85      console.info(`result=${result}`)
86      reply.writeString(result)
87    } else {
88      console.info(`unknown request code`)
89    }
90    return true
91  }
92}
93```
94