一文教你快速了解鸿蒙分布式调度并开发数据库相关应用

想了解更多内容,文教请访问:
和华为官方合作共建的快速鸿蒙技术社区
https://harmonyos.51cto.com
1. 介绍
开发者在应用中集成分布式调度能力,通过调用指定能力的解鸿据库分布式接口,实现跨设备能力调度。蒙分根据Ability模板及意图的布式并开不同,分布式任务调度向开发者提供六种能力:启动远程FA(Feature Ability)、调度启动远程PA(Particle Ability)、发数关闭远程PA、相关连接远程PA、应用断开连接远程PA和FA跨设备迁移。文教分布式任务调度的快速详细介绍可以参考分布式任务调度。
🕮 说明
实现远程启动FA,解鸿据库需要至少两个设备处于同一个分布式网络中,蒙分可以通过如下操作实现:
1. 所有设备接入同一网络;
2. 所有设备登录相同华为帐号;
3. 所有设备上开启"设置->更多连接->多设备协同 "。布式并开
本教程以"基于分布式调度远程启动FA"为例,调度结合权限申请、Button事件响应、获取设备列表、远程启动FA的开发过程,让您快速了解分布式调度能力。
2. 申请所需要的权限
在entry\src\main\config.json中申请以下4个权限:
1. ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE:用于允许监听分布式组网内的设备状态变化。
2. ohos.permission.GET_DISTRIBUTED_DEVICE_INFO:用于允许获取分布式组网内的设备列表和设备信息。b2b供应网
3. ohos.permission.GET_BUNDLE_INFO:用于查询其他应用的信息。
4. ohos.permission.DISTRIBUTED_DATASYNC:用于允许不同设备间的数据交换。
示例代码如下:
module": { ...... "reqPermissions": [ { "name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" }, { "name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" }, { "name": "ohos.permission.GET_BUNDLE_INFO" }, { "name": "ohos.permission.DISTRIBUTED_DATASYNC" } ] }此外,还需要在实现Ability的代码中显式声明需要使用多设备协同访问的权限,示例代码如下:
public class MainAbility extends Ability { @Override public void onStart(Intent intent) { requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"},0); super.onStart(intent); } }3. 实现一个Button,响应点击事件
在MainAbilitySlice.java中开发一个页面,并在页面上绘制一个Button,示例代码如下:
@Override public void onStart(Intent intent) { super.onStart(intent); DirectionalLayout layout = new DirectionalLayout(this); ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT); layout.setLayoutConfig(config); Button btn = new Button(getContext()); ShapeElement buttonBg = new ShapeElement(); buttonBg.setRgbColor(new RgbColor(0, 125, 255)); buttonBg.setCornerRadius(25); btn.setBackground(buttonBg); ComponentContainer.LayoutConfig btnConfig = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT); btn.setLayoutConfig(btnConfig); btn.setTextSize(50); btn.setPadding(10, 10, 10, 10); btn.setText("Start Remote FA"); layout.addComponent(btn); btn.setClickedListener(component -> { // 处理按钮响应,详情请见第3步,获取设备列表。 }); super.setUIContent(layout); }4. 获取设备列表
在远程启动FA按钮的响应里面实现设备列表的获取,使用DeviceManager.getDeviceList(int flag)获取设备列表,flag通过传入DeviceInfo.FLAG_GET_ONLINE_DEVICE查询所有分布式网络中的在线设备,通过解析返回的DeviceInfo列表对象,获取待被远程启动的FA的设备的deviceId。示例代码如下:
btn.setClickedListener(component -> { // 处理按钮响应,获取在线设备列表 List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); for (DeviceInfo deviceInfo : deviceInfoList) { // 远程启动FA,详情请见第4步 } });5. 远程启动FA
构建用于远程启动FA的云南idc服务商Intent,并远程启动FA。其中的BUNDLE_NAME和ABILITY_NAME为全局变量,表示需要启动的远程FA的BundleName(包名称)和AbilityName(待启动的Ability名称)。示例代码如下:
// 远程启动FA Intent remoteIntent = new Intent(); // 指定待启动FA的bundleName和abilityName // 例如:BUNDLE_NAME = "com.huawei.codelab" // ABILITY_NAME = "com.huawei.codelab.MainAbility" // 设置分布式标记,表明当前涉及分布式能力 Operation operation = new Intent.OperationBuilder().withDeviceId(deviceInfo.getDeviceId()) .withBundleName(BUNDLE_NAME) .withAbilityName(ABILITY_NAME) .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); remoteIntent.setOperation(operation); try { // 目标设备是否包含指定FA List<AbilityInfo> abilityInfoList = getBundleManager().queryAbilityByIntent(remoteIntent, 0, 0); if (abilityInfoList != null && !abilityInfoList.isEmpty()) { startAbility(remoteIntent); } } catch (RemoteException e) { // 处理异常 }6. 完整示例
以手机为例,点击页面按钮,会拉起同一网络中的其余使用同一华为帐号登录的手机上的指定FA,此处需要至少两台手机进行验证。实现效果如下:

示例代码如下:
import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.aafwk.content.Operation; import ohos.agp.colors.RgbColor; import ohos.agp.components.Button; import ohos.agp.components.ComponentContainer; import ohos.agp.components.DirectionalLayout; import ohos.agp.components.element.ShapeElement; import ohos.bundle.AbilityInfo; import ohos.distributedschedule.interwork.DeviceInfo; import ohos.distributedschedule.interwork.DeviceManager; import ohos.rpc.RemoteException; import java.util.List; public class MainAbilitySlice extends AbilitySlice { //远程启动FA的BundleName ,请自行填写 private static final String BUNDLE_NAME = "com.huawei.codelab"; // 远程启动FA的AbilityName,请自行填写 private static final String ABILITY_NAME = "com.huawei.codelab.MainAbility"; @Override public void onStart(Intent intent) { super.onStart(intent); DirectionalLayout layout = new DirectionalLayout(this); ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig( ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT); layout.setLayoutConfig(config); Button btn = new Button(getContext()); ShapeElement buttonBg = new ShapeElement(); buttonBg.setRgbColor(new RgbColor(0, 125, 255)); buttonBg.setCornerRadius(25); btn.setBackground(buttonBg); ComponentContainer.LayoutConfig btnConfig = new ComponentContainer.LayoutConfig( ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT); btn.setLayoutConfig(btnConfig); btn.setTextSize(50); btn.setPadding(10, 10, 10, 10); btn.setText("Start Remote FA"); layout.addComponent(btn); btn.setClickedListener(component -> { // 处理按钮响应,获取在线设备列 List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE); for (DeviceInfo deviceInfo : deviceInfoList) { // 远程启动FA Intent remoteIntent = new Intent(); Operation operation = new Intent.OperationBuilder() .withDeviceId(deviceInfo.getDeviceId()) .withBundleName(BUNDLE_NAME) .withAbilityName(ABILITY_NAME) .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) .build(); remoteIntent.setOperation(operation); try { List<AbilityInfo> abilityInfoList = getBundleManager().queryAbilityByIntent(remoteIntent, 0, 0); if (abilityInfoList != null && !abilityInfoList.isEmpty()) { startAbility(remoteIntent); } } catch (RemoteException e) { // 处理异常 } } }); super.setUIContent(layout); } }🕮 说明
以上代码仅demo演示参考使用,产品化的代码需要考虑数据校验和国际化。
想了解更多内容,请访问:
和华为官方合作共建的鸿蒙技术社区
https://harmonyos.51cto.com

本文地址:http://www.bzuk.cn/news/63f31499622.html
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。