当前位置:   article > 正文

华为Harmony鸿蒙开发笔记六:Ability跨设备分布式任务调度_connectpaintent.setoperation(operation);connectabi

connectpaintent.setoperation(operation);connectability(connectpaintent,conn)

目前来看,鸿蒙应用的跨设备分布式调度并不复杂,跟在本应用内调用没有太大却区别,只是多了权限管理和设备管理的相关参数和配置。

这里我将官方文档的代码整理一下,等待以后验证。

首先配置权限,根据已经验证了的DataAbility可知,权限是配置在请求方的Ability的配置文件里的

  1. {
  2. "skills": [
  3. {
  4. "entities": [
  5. "entity.system.home"
  6. ],
  7. "actions": [
  8. "action.system.home"
  9. ]
  10. }
  11. ],
  12. "orientation": "unspecified",
  13. "name": "com.example.distributeddemo.MainAbility",
  14. "icon": "$media:icon",
  15. "description": "$string:mainability_description",
  16. "label": "DistributedDemo",
  17. "type": "page",
  18. "launchType": "standard",
  19. "reqPermissions": [
  20. {
  21. "name": "ohos.permission.DISTRIBUTED_DATASYNC"
  22. }
  23. ]
  24. }

上面reqPermissions就是配置权限,使该Ability支持任务调度,然后就是改造AbilitySlice

首先要实现IAbilityContinuation接口,并实现接口方法,并在onStart里显式申明权限

  1. public class SampleSlice extends AbilitySlice implements IAbilityContinuation {
  2. @Override
  3. public void onStart(Intent intent) {
  4. // 开发者显示声明需要使用的权限
  5. requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"}, 0);
  6. super.onStart(intent);
  7. }
  8. }

然后编写UI,添加几个按钮:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:orientation="vertical">
  7. <Button
  8. ohos:id="$+id:btn_start_remote_fa"
  9. ohos:width="match_content"
  10. ohos:height="match_content"
  11. ohos:text="远程启动FA"
  12. ohos:text_size="19fp"
  13. ohos:text_color="#FFFFFF"
  14. ohos:top_padding="8vp"
  15. ohos:layout_alignment="center"
  16. ohos:bottom_padding="8vp"
  17. ohos:right_padding="70vp"
  18. ohos:left_padding="70vp"
  19. ohos:top_margin="10vp"
  20. ohos:background_element="$graphic:background_button"
  21. ohos:center_in_parent="true"
  22. ohos:bottom_margin="10vp"/>
  23. <Button
  24. ohos:id="$+id:btn_continue_remote_fa"
  25. ohos:width="match_content"
  26. ohos:height="match_content"
  27. ohos:text="远程迁移FA"
  28. ohos:text_size="19fp"
  29. ohos:text_color="#FFFFFF"
  30. ohos:top_padding="8vp"
  31. ohos:layout_alignment="center"
  32. ohos:bottom_padding="8vp"
  33. ohos:right_padding="70vp"
  34. ohos:left_padding="70vp"
  35. ohos:top_margin="10vp"
  36. ohos:background_element="$graphic:background_button"
  37. ohos:center_in_parent="true"
  38. ohos:bottom_margin="10vp"/>
  39. <Button
  40. ohos:id="$+id:btn_start_remote_pa"
  41. ohos:width="match_content"
  42. ohos:height="match_content"
  43. ohos:text="远程启动PA"
  44. ohos:text_size="19fp"
  45. ohos:text_color="#FFFFFF"
  46. ohos:top_padding="8vp"
  47. ohos:layout_alignment="center"
  48. ohos:bottom_padding="8vp"
  49. ohos:right_padding="70vp"
  50. ohos:left_padding="70vp"
  51. ohos:top_margin="10vp"
  52. ohos:background_element="$graphic:background_button"
  53. ohos:center_in_parent="true"
  54. ohos:bottom_margin="10vp"/>
  55. <Button
  56. ohos:id="$+id:btn_stop_remote_pa"
  57. ohos:width="match_content"
  58. ohos:height="match_content"
  59. ohos:text="远程关闭PA"
  60. ohos:text_size="19fp"
  61. ohos:text_color="#FFFFFF"
  62. ohos:top_padding="8vp"
  63. ohos:layout_alignment="center"
  64. ohos:bottom_padding="8vp"
  65. ohos:right_padding="70vp"
  66. ohos:left_padding="70vp"
  67. ohos:top_margin="10vp"
  68. ohos:background_element="$graphic:background_button"
  69. ohos:center_in_parent="true"
  70. ohos:bottom_margin="10vp"/>
  71. <Button
  72. ohos:id="$+id:btn_connect_remote_pa"
  73. ohos:width="match_content"
  74. ohos:height="match_content"
  75. ohos:text="连接远程PA"
  76. ohos:text_size="19fp"
  77. ohos:text_color="#FFFFFF"
  78. ohos:top_padding="8vp"
  79. ohos:layout_alignment="center"
  80. ohos:bottom_padding="8vp"
  81. ohos:right_padding="70vp"
  82. ohos:left_padding="70vp"
  83. ohos:top_margin="10vp"
  84. ohos:background_element="$graphic:background_button"
  85. ohos:center_in_parent="true"
  86. ohos:bottom_margin="10vp"/>
  87. <Button
  88. ohos:id="$+id:btn_control_remote_pa"
  89. ohos:width="match_content"
  90. ohos:height="match_content"
  91. ohos:text="控制连接PA"
  92. ohos:text_size="19fp"
  93. ohos:text_color="#FFFFFF"
  94. ohos:top_padding="8vp"
  95. ohos:layout_alignment="center"
  96. ohos:bottom_padding="8vp"
  97. ohos:right_padding="70vp"
  98. ohos:left_padding="70vp"
  99. ohos:top_margin="10vp"
  100. ohos:background_element="$graphic:background_button"
  101. ohos:center_in_parent="true"
  102. ohos:bottom_margin="10vp"/>
  103. <Button
  104. ohos:id="$+id:btn_disconnect_remote_pa"
  105. ohos:width="match_content"
  106. ohos:height="match_content"
  107. ohos:text="断开远程PA"
  108. ohos:text_size="19fp"
  109. ohos:text_color="#FFFFFF"
  110. ohos:top_padding="8vp"
  111. ohos:layout_alignment="center"
  112. ohos:bottom_padding="8vp"
  113. ohos:right_padding="70vp"
  114. ohos:left_padding="70vp"
  115. ohos:top_margin="10vp"
  116. ohos:background_element="$graphic:background_button"
  117. ohos:center_in_parent="true"
  118. ohos:bottom_margin="10vp"/>
  119. </DirectionalLayout>

初始化按钮,并添加点击事件:

  1. private MyRemoteProxy mProxy = null;
  2. private Button btnConnectRemotePA;
  3. private Button btnControlRemotePA;
  4. private Button btnContinueRemoteFA;
  5. @Override
  6. public void onStart(Intent intent) {
  7. super.onStart(intent);
  8. super.setUIContent(ResourceTable.Layout_ability_main);
  9. requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"}, 0);
  10. InsideListener listener = new InsideListener();
  11. findComponentById(ResourceTable.Id_btn_start_remote_fa).setClickedListener(listener);
  12. findComponentById(ResourceTable.Id_btn_start_remote_pa).setClickedListener(listener);
  13. findComponentById(ResourceTable.Id_btn_stop_remote_pa).setClickedListener(listener);
  14. btnContinueRemoteFA = (Button) findComponentById(ResourceTable.Id_btn_continue_remote_fa);
  15. btnContinueRemoteFA.setClickedListener(listener);
  16. btnConnectRemotePA = (Button) findComponentById(ResourceTable.Id_btn_connect_remote_pa);
  17. btnConnectRemotePA.setClickedListener(listener);
  18. btnControlRemotePA = (Button) findComponentById(ResourceTable.Id_btn_control_remote_pa);
  19. btnControlRemotePA.setClickedListener(listener);
  20. findComponentById(ResourceTable.Id_btn_disconnect_remote_pa).setClickedListener(listener);
  21. }
  22. class InsideListener implements Component.ClickedListener {
  23. @Override
  24. public void onClick(Component component) {
  25. switch (component.getId()) {
  26. case ResourceTable.Id_btn_start_remote_fa:
  27. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_start_remote_fa");
  28. break;
  29. case ResourceTable.Id_btn_continue_remote_fa:
  30. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_continue_remote_fa");
  31. break;
  32. case ResourceTable.Id_btn_start_remote_pa:
  33. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_start_remote_pa");
  34. break;
  35. case ResourceTable.Id_btn_stop_remote_pa:
  36. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_stop_remote_pa");
  37. break;
  38. case ResourceTable.Id_btn_connect_remote_pa:
  39. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_connect_remote_pa");
  40. break;
  41. case ResourceTable.Id_btn_control_remote_pa:
  42. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_control_remote_pa");
  43. break;
  44. case ResourceTable.Id_btn_disconnect_remote_pa:
  45. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_disconnect_remote_pa");
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. }

通过设备管理DeviceManager提供的getDeviceList接口获取设备列表,并选择一个设备:

  1. // ISelectResult是一个自定义接口,用来处理指定设备deviceId后执行的行为
  2. interface ISelectResult {
  3. void onSelectResult(String deviceId);
  4. }
  5. // 获得设备列表,开发者可在得到的在线设备列表中选择目标设备执行操作
  6. private void scheduleRemoteAbility(ISelectResult listener) {
  7. // 调用DeviceManager的getDeviceList接口,通过FLAG_GET_ONLINE_DEVICE标记获得在线设备列表
  8. List<DeviceInfo> onlineDevices = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
  9. // 判断组网设备是否为空
  10. if (onlineDevices.isEmpty()) {
  11. listener.onSelectResult(null);
  12. return;
  13. }
  14. int numDevices = onlineDevices.size();
  15. ArrayList<String> deviceIds = new ArrayList<>(numDevices);
  16. ArrayList<String> deviceNames = new ArrayList<>(numDevices);
  17. onlineDevices.forEach((device) -> {
  18. deviceIds.add(device.getDeviceId());
  19. deviceNames.add(device.getDeviceName());
  20. });
  21. // 以选择首个设备作为目标设备为例
  22. // 开发者也可按照具体场景,通过别的方式进行设备选择
  23. String selectDeviceId = deviceIds.get(0);
  24. listener.onSelectResult(selectDeviceId);
  25. }

各个按钮对应的点击事件:

  1. class InsideListener implements Component.ClickedListener {
  2. @Override
  3. public void onClick(Component component) {
  4. switch (component.getId()) {
  5. case ResourceTable.Id_btn_start_remote_fa:
  6. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_start_remote_fa");
  7. // 启动远程PA
  8. scheduleRemoteAbility(new ISelectResult() {
  9. @Override
  10. public void onSelectResult(String deviceId) {
  11. if (deviceId != null) {
  12. Intent intent = new Intent();
  13. // 通过scheduleRemoteAbility指定目标设备deviceId
  14. // 指定待启动FA的bundleName和abilityName
  15. // 例如:bundleName = "com.huawei.helloworld"
  16. // abilityName = "com.huawei.helloworld.SampleFeatureAbility"
  17. // 设置分布式标记,表明当前涉及分布式能力
  18. Operation operation = new Intent.OperationBuilder()
  19. .withDeviceId(deviceId)
  20. .withBundleName("目标bundleName")
  21. .withAbilityName("目标abilityName")
  22. .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
  23. .build();
  24. intent.setOperation(operation);
  25. // 通过AbilitySlice包含的startAbility接口实现跨设备启动FA
  26. startAbility(intent);
  27. }
  28. }
  29. });
  30. break;
  31. case ResourceTable.Id_btn_continue_remote_fa:
  32. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_continue_remote_fa");
  33. // 用户选择设备后实现业务迁移
  34. scheduleRemoteAbility(new ISelectResult() {
  35. @Override
  36. public void onSelectResult(String deviceId) {
  37. continueAbility(deviceId);
  38. }
  39. });
  40. break;
  41. case ResourceTable.Id_btn_start_remote_pa:
  42. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_start_remote_pa");
  43. // 启动远程PA
  44. scheduleRemoteAbility(new ISelectResult() {
  45. @Override
  46. public void onSelectResult(String deviceId) {
  47. if (deviceId != null) {
  48. Intent intentToStartPA = new Intent();
  49. // bundleName和abilityName与待启动PA对应
  50. // 例如:bundleName = "com.huawei.helloworld"
  51. // abilityName = "com.huawei.helloworld.SampleParticleAbility"
  52. Operation operation = new Intent.OperationBuilder()
  53. .withDeviceId(deviceId)
  54. .withBundleName("目标bundleName")
  55. .withAbilityName("目标abilityName")
  56. .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
  57. .build();
  58. intentToStartPA.setOperation(operation);
  59. startAbility(intentToStartPA);
  60. }
  61. }
  62. });
  63. break;
  64. case ResourceTable.Id_btn_stop_remote_pa:
  65. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_stop_remote_pa");
  66. scheduleRemoteAbility(new ISelectResult() {
  67. @Override
  68. public void onSelectResult(String deviceId) {
  69. if (deviceId != null) {
  70. Intent intentToStopPA = new Intent();
  71. // bundleName和abilityName与待关闭PA对应
  72. // 例如:bundleName = "com.huawei.helloworld"
  73. // abilityName = "com.huawei.helloworld.SampleParticleAbility"
  74. Operation operation = new Intent.OperationBuilder()
  75. .withDeviceId(deviceId)
  76. .withBundleName("目标bundleName")
  77. .withAbilityName("目标abilityName")
  78. .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
  79. .build();
  80. intentToStopPA.setOperation(operation);
  81. stopAbility(intentToStopPA);
  82. }
  83. }
  84. });
  85. break;
  86. case ResourceTable.Id_btn_connect_remote_pa:
  87. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_connect_remote_pa");
  88. scheduleRemoteAbility(new ISelectResult() {
  89. @Override
  90. public void onSelectResult(String deviceId) {
  91. if (deviceId != null) {
  92. Intent connectPAIntent = new Intent();
  93. // bundleName和abilityName与待连接的PA一一对应
  94. // 例如:bundleName = "com.huawei.helloworld"
  95. // abilityName = "com.huawei.helloworld.SampleParticleAbility"
  96. Operation operation = new Intent.OperationBuilder()
  97. .withDeviceId(deviceId)
  98. .withBundleName("目标bundleName")
  99. .withAbilityName("目标abilityName")
  100. .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
  101. .build();
  102. connectPAIntent.setOperation(operation);
  103. connectAbility(connectPAIntent, conn);
  104. }
  105. }
  106. });
  107. break;
  108. case ResourceTable.Id_btn_control_remote_pa:
  109. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_control_remote_pa");
  110. if (mProxy != null) {
  111. int ret = -1;
  112. try {
  113. ret = mProxy.plus(10, 20);
  114. } catch (RemoteException e) {
  115. e.printStackTrace();
  116. }
  117. btnControlRemotePA.setText("控制连接 result = " + ret);
  118. }
  119. break;
  120. case ResourceTable.Id_btn_disconnect_remote_pa:
  121. HiLog.info(LABEL_LOG, "MainAbilitySlice btn_disconnect_remote_pa");
  122. btnConnectRemotePA.setText("连接远程PA");
  123. btnControlRemotePA.setText("控制连接PA");
  124. disconnectAbility(conn);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. }

最后,FA跨设备迁移的生命周期方法:

流程参见;https://blog.csdn.net/y280903468/article/details/111770977

  1. @Override
  2. public boolean onSaveData(IntentParams saveData) {
  3. String exampleData = String.valueOf(System.currentTimeMillis());
  4. saveData.setParam("continueParam", exampleData);
  5. return true;
  6. }
  7. @Override
  8. public boolean onRestoreData(IntentParams restoreData) {
  9. // 远端FA迁移传来的状态数据,开发者可以按照特定的场景对这些数据进行处理
  10. Object data = restoreData.getParam("continueParam");
  11. return true;
  12. }
  13. @Override
  14. public void onCompleteContinuation(int result) {
  15. btnContinueRemoteFA.setText("ContinueAbility Done");
  16. }

 

https://download.csdn.net/download/y280903468/14004469

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号