在 HarmonyOS 中,分布式任务调度平台对搭载 HarmonyOS 的多设备构筑的“超级虚拟终端”提供统一的组件管理能力,为应用定义统一的能力基线、接口形式、数据结构、服务描述语言,屏蔽硬件差异;支持远程启动、远程调用、业务无缝迁移等分布式任务。
①
远程调用 PA/FA,开发者需要在 Intent 中设置支持分布式的标记(例如:Intent.FLAG_ABILITYSLICE_MULTI_DEVICE 表示该应用支持分布式调度),否则将无法获得分布式能力。
②
开发者通过在 config.json 中的 reqPermissions 字段里添加权限申请,以获取跨设备连接的能力和分布式数据传输的权限:
{"name": "ohos.permission.servicebus.ACCESS_SERVICE"}
{"name": "ohos.permission.servicebus.DISTRIBUTED_DATASYNC"}
{"name": "com.huawei.hwddmp.servicebus.BIND_SERVICE"}
{"name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"},
{"name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" },
{ "name": "ohos.permission.GET_BUNDLE_INFO"}
注意:
还需要在开发的时候,要在 Ability 里主动声明,要用到的权限。
FA(Feature Ability,Page 模板的 Ability)的调用支持启动和迁移行为,在进行调度时:
下面以设备 A(本地设备)和设备 B(远端设备)为例,介绍下面我们要实现的场景:
-
设备 A 启动设备 B 的 FA:
在设备 A 上通过本地应用提供的启动按钮,启动设备 B 上对应的 FA。
-
设备 A 的 FA 迁移至设备 B:
设备 A 上通过本地应用提供的迁移按钮,将设备 A 的业务无缝迁移到设备 B 中。
-
设备 A 的 FA 迁移至设备 B,
还可以实现主动撤回迁移。
①启动远程 FA
startAbility(Intent intent
)
接口提供启动指定设备上 FA 和 PA 的能力,Intent 中指定待启动 FA 的设备 deviceId、bundleName 和 abilityName。
②迁移 FA
continueAbility(String
deviceId
)
接口提供将本地 FA 迁移到指定设备上的能力。
continueAbilityReversibly(String
deviceId
)
接口提供将本地 FA 迁移到指定设备上的能力,这种迁移可撤回,reverseContinueAbility() 接口提供撤回迁移的能力。
①编程实现上面场景的界面
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:migration_btn_01"
ohos:height="match_content"
ohos:width="300vp"
ohos:text="1.启动远程设备的FA"
ohos:text_size="20fp"
ohos:text_color="#ffffff"
ohos:background_element="$graphic:button_bg"
ohos:layout_alignment="horizontal_center"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:left_padding="40vp"
ohos:right_padding="40vp"
ohos:top_margin="20vp"
/>
<Button
ohos:id="$+id:migration_btn_02"
ohos:height="match_content"
ohos:width="300vp"
ohos:text="2.迁移到远程设备"
ohos:text_size="20fp"
ohos:text_color="#ffffff"
ohos:background_element="$graphic:button_bg"
ohos:layout_alignment="horizontal_center"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:left_padding="40vp"
ohos:right_padding="40vp"
ohos:top_margin="20vp"
/>
<Button
ohos:id
="$+id:migration_btn_03"
ohos:height="match_content"
ohos:width="300vp"
ohos:text="3.可迁回的迁移远程设备"
ohos:text_size="20fp"
ohos:text_color="#ffffff"
ohos:background_element="$graphic:button_bg"
ohos:layout_alignment="horizontal_center"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:left_padding="40vp"
ohos:right_padding="40vp"
ohos:top_margin="20vp"
/>
DirectionalLayout>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid ohos:color="#007DFF"/>
<corners ohos:radius="40"/>
shape>
MigrationAbility 和 MigrationBackAbility:
// 调用AbilitySlice模板实现一个用于控制基础功能的FA
// Ability和AbilitySlice类均需要实现IAbilityContinuation及其方法,才可以实现FA迁移。AbilitySlice的代码示例如下
public class SampleSlice extends AbilitySlice implements IAbilityContinuation {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(layout);
}
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#00ffff"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_title"
ohos:height="match_content"
ohos:width="250vp"
ohos:background_element="#0088bb"
ohos:layout_alignment="horizontal_center"
ohos:text="下面是一个可编辑的文本框"
ohos:text_size="50"
ohos:padding="5vp"
ohos:top_margin="30vp"
/>
<TextField
ohos:id="$+id:textfield_back"
ohos:height="250vp"
ohos:width="250vp"
ohos:hint="请输入..."
ohos:layout_alignment="horizontal_center"
ohos:background_element="#ffffff"
ohos:text_color="#888888"
ohos:text_size="20fp"
ohos:padding="5vp"
/>
<Button
ohos:id="$+id:migration_button"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="点击迁移"
ohos:text_size="20fp"
ohos:text_color