harmony 鸿蒙Connecting to a ServiceAbility

  • 2023-02-03
  • 浏览 (354)

Connecting to a ServiceAbility

If 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. 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.

Table 1 IAbilityConnection APIs

API Description
onConnect() Callback invoked when the ServiceAbility is connected.
onDisconnect() Callback invoked when the ServiceAbility is disconnected.
onFailed() Callback invoked when the connection to the ServiceAbility fails.

The following sample code enables the PageAbility to create connection callback instances and connect to the local ServiceAbility:

import rpc from "@ohos.rpc"
import promptAction from '@ohos.promptAction'
import featureAbility from '@ohos.ability.featureAbility'
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';

let option: common.ConnectOptions = {
  onConnect: (element, proxy) => {
    console.info(`onConnectLocalService onConnectDone`)
    if (proxy === null) {
      promptAction.showToast({
        message: "Connect service failed"
      })
      return
    }
    let data = rpc.MessageParcel.create()
    let reply = rpc.MessageParcel.create()
    let option = new rpc.MessageOption()
    data.writeInterfaceToken("connect.test.token")
    proxy.sendRequest(0, data, reply, option)
    promptAction.showToast({
      message: "Connect service success"
    })
  },
  onDisconnect: (element) => {
    console.info(`onConnectLocalService onDisconnectDone element:${element}`)
    promptAction.showToast({
      message: "Disconnect service success"
    })
  },
  onFailed: (code) => {
    console.info(`onConnectLocalService onFailed errCode:${code}`)
    promptAction.showToast({
      message: "Connect local service onFailed"
    })
  }
};

let request: Want = {
  bundleName: "com.example.myapplication",
  abilityName: "com.example.myapplication.ServiceAbility",
}
let connId = featureAbility.connectAbility(request, option)

When 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.

The following sample code shows how the ServiceAbility returns itself to the caller:

import rpc from "@ohos.rpc"

class FirstServiceAbilityStub extends rpc.RemoteObject {
  constructor(des: Object) {
    if (typeof des === 'string') {
      super(des)
    } else {
      return
    }
  }

  onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption) {
    console.info(`onRemoteRequest called`)
    if (code === 1) {
      let string = data.readString()
      console.info(`string=${string}`)
      let result = Array.from(string).sort().join('')
      console.info(`result=${result}`)
      reply.writeString(result)
    } else {
      console.info(`unknown request code`)
    }
    return true
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞