OpenHarmony应用如何跨设备传输分布式数据对象

301 篇文章 0 订阅
19 篇文章 0 订阅

简介:

分布式组网成功后,应用间可通过分布式数据对象进行消息的传输,分布式数据对象的相关接口可以使用 @ohos.data.uniformTypeDescriptor 来实现。

文档环境:

  • 开发环境:Windows 10 专业版
  • DevEco Studio 版本:DevEco Studio 4.0Release (4.0.0.600)
  • SDK 版本:4.1.3.3 (full sdk)
  • API 版本:Version 11
  • 开发板型号:DAYU200(RK3568)
  • 系统版本:OpenHarmony 4.1.3.5

演示 demo:

  • 新建一个 Stage 框架的 demo 工程。

  • 实现了一个 DataObject 单例对象:通过接口 distributedDataObject.createDistributedObject 创建分布式对象实例,该对象中包含了属性值"from"(具体使用时可根据实际的场景需求进行属性的自定义)。同时定义了 update 和 clear 方法,用于对该分布式对象实例的属性值更新和清除。

  • 上面实现的 DataObject 单例,在应用入口创建时通过 setSessionId 接口设置同步的 sessionId,多个设备间的对象通过设置相同的 sessionId,就能实现自动同步。

  • 使用时,可以通过调用 DataObject.update 进行属性的更新,通过 DataObject.distributedObject[“from”]的方式访问分布式数据对象。
    demo 运行效果:
    image.png

核心代码如下:
先将分布式数据对象封装为一个单例:

```
import distributedDataObject from '@ohos.data.distributedDataObject';
import common from '@ohos.app.ability.common';
class DataObject {
public distributedObject: distributedDataObject.DistributedObject;
constructor() {
this.distributedObject = distributedDataObject.createDistributedObject( {
from: ''
})
}
setSessionId(context: common.UIAbilityContext, sessionId: string): void {
console.log(\`setSessionId\`)
this.distributedObject.setSessionId(sessionId)
}

update(from: string) {
console.log(\`doUpdate,\${from}\`)
this.distributedObject['from'] = from;
}
}

export default new DataObject();

```

该单例在应用入口创建时通过setSessionId接口设置同步的sessionId,多个设备间的对象通过设置相同的sessionId,就能实现自动同步。

import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import DataObject from '../model/DataObject';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';

const sessionId : string = "123456";

export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
//user grant
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
try {
  atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED\_DATASYNC']).then((data) => {
    DataObject.setSessionId(this.context, sessionId);
    console.log(\`data:\${JSON.stringify(data)}\`);
  }).catch((err: BusinessError) => {
    console.log(\`err:\${JSON.stringify(err)}\`);
  })
} catch (err) {
  console.log(\`catch err->\${JSON.stringify(err)}\`);
}
}

该分布式对象的使用

import DataObject from '../model/DataObject';

@Entry
@Component
struct Index {
@State getDisString: string = '';
@State setDisString: string = '444';

build() {
Row() {
Column() {
TextInput({
text: this.setDisString,
})
.width(436)
.onChange((value: string) => {
this.setDisString = value
})
Button("更新分布式数据对象")
.backgroundColor(0x317aff)
.fontSize(16)
.height(40)
.borderRadius(20)
.margin({ left: 24, right: 24, bottom: 24 })
.width(436)
.onClick(() => {
DataObject.update(this.setDisString);
})
Text(this.getDisString)
.fontSize(50)
.fontWeight(FontWeight.Bold)
Button("获取分布式数据对象")
.backgroundColor(0x317aff)
.fontSize(16)
.height(40)
.borderRadius(20)
.margin({ left: 24, right: 24, bottom: 24 })
.width(436)
.onClick(() => {
this.getDisString = DataObject.distributedObject["from"];
})  
}
  .width('100%')
}
.height('100%')
}
}

写在最后

●如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
●点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
●关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
●更多鸿蒙最新技术知识点,请移步前往小编:https://gitee.com/

在这里插入图片描述

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值