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

开发 分布式
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[389002]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

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:用于允许获取分布式组网内的设备列表和设备信息。

3. ohos.permission.GET_BUNDLE_INFO:用于查询其他应用的信息。

4. ohos.permission.DISTRIBUTED_DATASYNC:用于允许不同设备间的数据交换。

示例代码如下:

  1. module": {  
  2. ......  
  3.    "reqPermissions": [  
  4.     {  
  5.        "name""ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"  
  6.      },  
  7.      {  
  8.        "name""ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"  
  9.      },  
  10.      {  
  11.        "name""ohos.permission.GET_BUNDLE_INFO"  
  12.      },  
  13.      {  
  14.        "name""ohos.permission.DISTRIBUTED_DATASYNC"  
  15.      }  
  16.   ]  

此外,还需要在实现Ability的代码中显式声明需要使用多设备协同访问的权限,示例代码如下:

  1. public class MainAbility extends Ability {  
  2. @Override  
  3. public void onStart(Intent intent) {  
  4.     requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"},0);  
  5.     super.onStart(intent);   
  6.   }  

3. 实现一个Button,响应点击事件

在MainAbilitySlice.java中开发一个页面,并在页面上绘制一个Button,示例代码如下:

  1. @Override  
  2. public void onStart(Intent intent) {  
  3.     super.onStart(intent);  
  4.     DirectionalLayout layout = new DirectionalLayout(this);  
  5.     ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,   
  6.         ComponentContainer.LayoutConfig.MATCH_PARENT);  
  7.     layout.setLayoutConfig(config);  
  8.     Button btn = new Button(getContext());  
  9.     ShapeElement buttonBg = new ShapeElement();  
  10.     buttonBg.setRgbColor(new RgbColor(0, 125, 255));  
  11.     buttonBg.setCornerRadius(25);  
  12.     btn.setBackground(buttonBg);  
  13.     ComponentContainer.LayoutConfig btnConfig = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,   
  14.         ComponentContainer.LayoutConfig.MATCH_CONTENT);  
  15.     btn.setLayoutConfig(btnConfig);  
  16.     btn.setTextSize(50);  
  17.     btn.setPadding(10, 10, 10, 10);  
  18.     btn.setText("Start Remote FA");  
  19.     layout.addComponent(btn);  
  20.     btn.setClickedListener(component -> {  
  21.        // 处理按钮响应,详情请见第3步,获取设备列表。  
  22.     });  
  23.     super.setUIContent(layout);  

4. 获取设备列表

在远程启动FA按钮的响应里面实现设备列表的获取,使用DeviceManager.getDeviceList(int flag)获取设备列表,flag通过传入DeviceInfo.FLAG_GET_ONLINE_DEVICE查询所有分布式网络中的在线设备,通过解析返回的DeviceInfo列表对象,获取待被远程启动的FA的设备的deviceId。示例代码如下:

  1. btn.setClickedListener(component -> {  
  2. // 处理按钮响应,获取在线设备列表       
  3. List<DeviceInfo> deviceInfoList =   
  4. DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);  
  5.     for (DeviceInfo deviceInfo : deviceInfoList) {   
  6.         // 远程启动FA,详情请见第4步  
  7.     }  
  8. }); 

5. 远程启动FA

构建用于远程启动FA的Intent,并远程启动FA。其中的BUNDLE_NAME和ABILITY_NAME为全局变量,表示需要启动的远程FA的BundleName(包名称)和AbilityName(待启动的Ability名称)。示例代码如下:

  1. // 远程启动FA  
  2. Intent remoteIntent = new Intent();  
  3. // 指定待启动FA的bundleName和abilityName  
  4. // 例如:BUNDLE_NAME = "com.huawei.codelab"  
  5. //       ABILITY_NAME = "com.huawei.codelab.MainAbility"  
  6. // 设置分布式标记,表明当前涉及分布式能力  
  7. Operation operation = new Intent.OperationBuilder().withDeviceId(deviceInfo.getDeviceId())  
  8.     .withBundleName(BUNDLE_NAME)  
  9.     .withAbilityName(ABILITY_NAME)  
  10.     .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)  
  11.     .build();  
  12. remoteIntent.setOperation(operation);  
  13. try {  
  14.     // 目标设备是否包含指定FA  
  15.     List<AbilityInfo> abilityInfoList = getBundleManager().queryAbilityByIntent(remoteIntent, 0, 0);  
  16.     if (abilityInfoList != null && !abilityInfoList.isEmpty()) {  
  17.         startAbility(remoteIntent);  
  18.     }  
  19. } catch (RemoteException e) {  
  20.     // 处理异常  

6. 完整示例

以手机为例,点击页面按钮,会拉起同一网络中的其余使用同一华为帐号登录的手机上的指定FA,此处需要至少两台手机进行验证。实现效果如下:

示例代码如下:

  1. import ohos.aafwk.ability.AbilitySlice;  
  2. import ohos.aafwk.content.Intent;  
  3. import ohos.aafwk.content.Operation;  
  4. import ohos.agp.colors.RgbColor;  
  5. import ohos.agp.components.Button;  
  6. import ohos.agp.components.ComponentContainer;  
  7. import ohos.agp.components.DirectionalLayout;  
  8. import ohos.agp.components.element.ShapeElement;  
  9. import ohos.bundle.AbilityInfo;  
  10. import ohos.distributedschedule.interwork.DeviceInfo;  
  11. import ohos.distributedschedule.interwork.DeviceManager;  
  12. import ohos.rpc.RemoteException;  
  13.   
  14. import java.util.List;  
  15.   
  16. public class MainAbilitySlice extends AbilitySlice {  
  17.     //远程启动FA的BundleName ,请自行填写  
  18.     private static final String BUNDLE_NAME = "com.huawei.codelab";  
  19.     // 远程启动FA的AbilityName,请自行填写  
  20.     private static final String ABILITY_NAME = "com.huawei.codelab.MainAbility";  
  21.   
  22.     @Override  
  23.     public void onStart(Intent intent) {  
  24.         super.onStart(intent);  
  25.         DirectionalLayout layout = new DirectionalLayout(this);  
  26.         ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(  
  27.             ComponentContainer.LayoutConfig.MATCH_PARENT,   
  28.             ComponentContainer.LayoutConfig.MATCH_PARENT);  
  29.         layout.setLayoutConfig(config);  
  30.         Button btn = new Button(getContext());  
  31.         ShapeElement buttonBg = new ShapeElement();  
  32.         buttonBg.setRgbColor(new RgbColor(0, 125, 255));  
  33.         buttonBg.setCornerRadius(25);  
  34.         btn.setBackground(buttonBg);  
  35.         ComponentContainer.LayoutConfig btnConfig = new ComponentContainer.LayoutConfig(  
  36.             ComponentContainer.LayoutConfig.MATCH_PARENT,   
  37.             ComponentContainer.LayoutConfig.MATCH_CONTENT);  
  38.         btn.setLayoutConfig(btnConfig);  
  39.         btn.setTextSize(50);  
  40.         btn.setPadding(10, 10, 10, 10);  
  41.         btn.setText("Start Remote FA");  
  42.         layout.addComponent(btn);  
  43.         btn.setClickedListener(component -> {  
  44.             // 处理按钮响应,获取在线设备列  
  45.             List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);  
  46.             for (DeviceInfo deviceInfo : deviceInfoList) {  
  47.                 // 远程启动FA  
  48.                 Intent remoteIntent = new Intent();  
  49.                 Operation operation = new Intent.OperationBuilder()  
  50.                     .withDeviceId(deviceInfo.getDeviceId())  
  51.                     .withBundleName(BUNDLE_NAME)  
  52.                     .withAbilityName(ABILITY_NAME)  
  53.                     .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)  
  54.                     .build();  
  55.                 remoteIntent.setOperation(operation);  
  56.                 try {  
  57.                     List<AbilityInfo> abilityInfoList = getBundleManager().queryAbilityByIntent(remoteIntent, 0, 0);  
  58.                     if (abilityInfoList != null && !abilityInfoList.isEmpty()) {  
  59.                         startAbility(remoteIntent);  
  60.                     }  
  61.                 } catch (RemoteException e) {  
  62.                     // 处理异常  
  63.                 }  
  64.             }  
  65.         });  
  66.         super.setUIContent(layout);  
  67.     }  

🕮 说明

以上代码仅demo演示参考使用,产品化的代码需要考虑数据校验和国际化。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-04-14 11:14:02

PostgreSQL分布式数据库

2023-07-31 21:56:54

哨兵系统redis

2022-02-20 09:56:28

TCPIP网络协议

2017-09-04 14:46:10

分布式事务问题

2020-12-22 10:02:53

ZabbixMySQL数据库

2022-12-21 08:40:05

限流器分布式限流

2016-10-25 14:35:05

分布式系统 存储

2020-01-03 09:00:00

数据库数据库管理金融

2020-10-28 11:15:24

EPaxos分布式性算法

2022-07-28 09:02:41

文件存储系统

2020-11-06 12:12:35

HarmonyOS

2015-06-30 12:49:27

HBaseNoSQL分布式

2023-05-11 08:26:56

2022-07-13 09:53:58

分布式开发

2021-08-16 09:55:41

鸿蒙HarmonyOS应用

2019-07-23 07:30:16

2019-08-07 10:44:28

MySQLGoogle

2021-01-15 13:18:39

数据模型领域模型代码

2017-10-20 13:39:29

分布式系统数据存储数据量

2023-12-27 07:40:43

HTTP服务器负载均衡
点赞
收藏

51CTO技术栈公众号