#鸿蒙学习大百科#如何接收其他设备通过分布式对象同步的资产?

如何接收其他设备通过分布式对象同步的资产?

HarmonyOS
2024-10-24 10:35:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
无敌大暴龙
import distributedDataObject from '@ohos.data.distributedDataObject';
import commonType from '@ohos.data.commonType';

class People {
  name: string|undefined
  age: number |undefined= 0
  attachment: commonType.Asset | undefined

  constructor(name: string|undefined, age: number|undefined) {
    this.name = name
    this.age = age
  }
}
@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("接收资产").onClick(() => {
        let people = new People(undefined,undefined)
        people.attachment = undefined
        let remoteObject: distributedDataObject.DataObject = distributedDataObject.create(getContext(this), people);
        remoteObject.on('change', (sessionId: string, fields: Array<string>) => {
          if (fields.includes('attachment')) {
            // 接收端监听到资产类型属性的数据变更时,代表其所描述的资产文件同步完成
            console.info('attachment synchronization completed');
          }
        });
        remoteObject.setSessionId('123456');
      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-10-24 17:07:05
相关问题