#鸿蒙通关秘籍#在多端协作场景中使用分布式数据对象的步骤有哪些?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
DL碧血黄沙

首先,调用startAbilityByCall接口启动对端Ability,在此之前需要先生成一个sessionId并通过分布式设备管理获取对端设备的networkId。然后,组装包含sessionId的want并启动该Ability。客户端响应后要创建一个分布式数据对象实例,并注册数据变更监听。最后将sessionId设置到对象中以加入组网。另外,被调用端被拉起后创建和恢复分布式数据对象时,需要同样设置sessionId以加入同步网络。

javascript import { AbilityConstant, Caller, common, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { distributedDataObject } from '@kit.ArkData'; import { distributedDeviceManager } from '@kit.DistributedServiceKit'; import { BusinessError } from '@kit.BasicServicesKit';

class Data { title: string | undefined; text: string | undefined;

constructor(title: string | undefined, text: string | undefined) { this.title = title; this.text = text; } }

const TAG = '[DistributedDataObject]'; let sessionId: string; let caller: Caller; let dataObject: distributedDataObject.DataObject;

export default class EntryAbility extends UIAbility { callRemote() { if (caller) { console.error(TAG + 'call remote already'); return; } let context = getContext(this) as common.UIAbilityContext;

sessionId = distributedDataObject.genSessionId();
let deviceId = getRemoteDeviceId();
let want: Want = {
  bundleName: 'com.example.collaboration',
  abilityName: 'EntryAbility',
  deviceId: deviceId,
  parameters: {
    'ohos.aafwk.param.callAbilityToForeground': true,
    'distributedSessionId': sessionId
  }
};
context.startAbilityByCall(want).then((res) => {
  caller = res;
});

}

createDataObject() { if (!caller) { console.error(TAG + 'call remote first'); return; } if (dataObject) { console.error(TAG + 'create dataObject already'); return; } let context = getContext(this) as common.UIAbilityContext; let data = new Data('The title', 'The text'); dataObject = distributedDataObject.create(context, data); dataObject.on('change', (sessionId, fields) => { fields.forEach((field) => { console.log(TAG + ${field}: ${dataObject[field]}); }); }); dataObject.setSessionId(sessionId); }

onCreate(want, launchParam) { if (want.parameters && want.parameters.distributedSessionId) { let data = new Data(undefined, undefined); dataObject = distributedDataObject.create(this.context, data); dataObject.on('change', (sessionId, fields) => { fields.forEach((field) => { console.log(TAG + ${field}: ${dataObject[field]}); }); }); let sessionId = want.parameters.distributedSessionId as string; dataObject.setSessionId(sessionId); } } }

function getRemoteDeviceId() { let deviceId = ""; try { let deviceManager = distributedDeviceManager.createDeviceManager('com.example.collaboration'); let devices = deviceManager.getAvailableDeviceListSync(); if (devices[0] && devices[0].networkId) { deviceId = devices[0].networkId; } } catch (e) { let err = e as BusinessError; console.error(TAG + get remote deviceId error, error code: ${err.code}, error message: ${err.message}); } return deviceId; }

分享
微博
QQ
微信
回复
3天前
相关问题
分布式跨设备组件启动几种场景
387浏览 • 1回复 待解决
分布式存储数据方法
399浏览 • 1回复 待解决
分布式键值数据使用分享
1156浏览 • 1回复 待解决
分布式恢复数据方法
411浏览 • 1回复 待解决
分布式数据疑惑吗?
3015浏览 • 1回复 待解决
求解分布式数据使用问题?
4363浏览 • 1回复 待解决