Skip to content
Menu
(◍ ´꒳` ◍)

  • Blog
  • Front Page
(◍ ´꒳` ◍)

鸿蒙45链接sever

By admin on 2021年10月6日2021年10月6日

连接 Service 如果 Service 需要与 Page Ability 或其他应用的 Service Ability 进行交互,则应创建用于 连接的 Connection。Service 支持其他 Ability 通过 connectAbility()方法与其进行连接。 在使用 connectAbility()处理回调时,需要传入目标 Service 的 Intent 与 IAbilityConnection 的实例。IAbilityConnection 提供了两个方法供开发者实现: onAbilityConnectDone()用来处理连接的回调,onAbilityDisconnectDone()用来处理断开连 接的回调。 连接 Service 的代码示例如下:

// 创建连接回调实例
private私人的 IAbilityConnection connection = new IAbilityConnection() {
// 连接到 Service 的回调
@Override
public void onAbilityConnectDone(Element电子Name elementName, IRemote遥远的Object iRemoteObject, int
resultCode) {
// Client 客户侧需要定义与 Service 侧相同的 IRemoteObject 实现类。开发者获取服务端传过来
IRemoteObject 对象,并从中解析出服务端传过来的信息。
}
// 断开与 Service 连接的回调
@Override
public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
}
};
// 连接 Service
connectAbility(intent, connection);

同时,Service 侧也需要在 onConnect()时返回 IRemoteObject,从而定义与 Service 进行
通信的接口。onConnect()需要返回一个 IRemoteObject 对象,HarmonyOS 提供了
IRemoteObject 的默认实现,用户可以通过继承 LocalRemoteObject 来创建自定义的实现
类。Service 侧把自身的实例返回给调用侧的代码示例如下:
// 创建自定义 IRemoteObject 实现类
private class MyRemoteObject extends Local本地的RemoteObject {
MyRemoteObject(){
}
}

// 把 IRemoteObject 返回给客户端
@Override
protected IRemoteObject onConnect(Intent intent) {
return new MyRemoteObject();
}

Category: 鸿蒙

文章导航

鸿蒙44停止sever
paper周杰伦翻译12

Related Posts

Docker进阶:redis集群3主3从002实战哈希槽分布式

9月 22, 2023
Read More

linux查看jdk安装路径java_home

10月 5, 2023
Read More

鸿蒙60注册 UserDataAbility

11月 3, 2021
Read More

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

近期文章

  • 实施002
  • 实施SQL001
  • 泛微oa001-字段
  • 硬件前端-海康威视摄像头
  • 使用Docker搭建Weblogic服务001

近期评论

    归档

    • 2023年10月
    • 2023年9月
    • 2023年8月
    • 2023年5月
    • 2023年4月
    • 2023年3月
    • 2021年11月
    • 2021年10月
    • 2021年9月
    • 2021年8月
    • 2021年5月

    分类

    • C
    • docker
    • iptable
    • ISIS
    • java
    • k8s
    • liunx
    • OpenCV py
    • ospf
    • solr
    • zabbix
    • 劳动法
    • 周杰伦
    • 格林法则
    • 等保
    • 鸿蒙

    其他操作

    • 登录
    • 条目feed
    • 评论feed
    • WordPress.org
    ©2025 (◍ ´꒳` ◍) | WordPress Theme by Superb WordPress Themes