• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 连接ServiceAbility
2
3
4如果ServiceAbility需要与PageAbility或其他应用的ServiceAbility进行交互,则须创建用于连接的Connection。ServiceAbility支持其他Ability通过connectAbility()方法与其进行连接。PageAbility的connectAbility方法定义在featureAbility中,ServiceAbility的connectAbility方法定义在particleAbility中。连接ServiceAbility的规则详见[组件启动规则](component-startup-rules.md)章节。在使用connectAbility()处理回调时,需要传入目标Service的Want与IAbilityConnection的实例。IAbilityConnection提供了以下方法供开发者实现。
5
6
7  **表1** IAbilityConnection接口说明
8
9| 接口名 | 描述 |
10| -------- | -------- |
11| onConnect() | 用于处理连接Service成功的回调。 |
12| onDisconnect() | 用来处理Service异常死亡的回调。 |
13| onFailed() | 用来处理连接Service失败的回调。 |
14
15
16PageAbility创建连接本地ServiceAbility回调实例的代码以及连接本地ServiceAbility的示例代码如下:
17
18```ts
19import rpc from "@ohos.rpc"
20import prompt from '@system.prompt'
21import featureAbility from '@ohos.ability.featureAbility'
22
23let option = {
24  onConnect: function onConnectCallback(element, proxy) {
25    console.info(`onConnectLocalService onConnectDone`)
26    if (proxy === null) {
27      prompt.showToast({
28        message: "Connect service failed"
29      })
30      return
31    }
32    let data = rpc.MessageParcel.create()
33    let reply = rpc.MessageParcel.create()
34    let option = new rpc.MessageOption()
35    data.writeInterfaceToken("connect.test.token")
36    proxy.sendRequest(0, data, reply, option)
37    prompt.showToast({
38      message: "Connect service success"
39    })
40  },
41  onDisconnect: function onDisconnectCallback(element) {
42    console.info(`onConnectLocalService onDisconnectDone element:${element}`)
43    prompt.showToast({
44      message: "Disconnect service success"
45    })
46  },
47  onFailed: function onFailedCallback(code) {
48    console.info(`onConnectLocalService onFailed errCode:${code}`)
49    prompt.showToast({
50      message: "Connect local service onFailed"
51    })
52  }
53}
54
55let request = {
56  bundleName: "com.example.myapplication",
57  abilityName: "com.example.myapplication.ServiceAbility",
58}
59let connId = featureAbility.connectAbility(request, option)
60```
61
62
63同时,Service侧也需要在onConnect()时返回IRemoteObject,从而定义与Service进行通信的接口。onConnect()需要返回一个IRemoteObject对象。OpenHarmony提供了IRemoteObject的默认实现,开发者可以通过继承rpc.RemoteObject来创建自定义的实现类。
64
65
66Service侧把自身的实例返回给调用侧的示例代码如下:
67
68```ts
69import rpc from "@ohos.rpc"
70
71class FirstServiceAbilityStub extends rpc.RemoteObject {
72  constructor(des: any) {
73    if (typeof des === 'string') {
74      super(des)
75    } else {
76      return
77    }
78  }
79
80  onRemoteRequest(code: number, data: any, reply: any, option: any) {
81    console.info(`onRemoteRequest called`)
82    if (code === 1) {
83      let string = data.readString()
84      console.info(`string=${string}`)
85      let result = Array.from(string).sort().join('')
86      console.info(`result=${result}`)
87      reply.writeString(result)
88    } else {
89      console.info(`unknown request code`)
90    }
91    return true
92  }
93}
94```
95