1024程序员节,分布式协同-你画我猜 原创 精华
rentututu
发布于 2021-10-19 17:03
浏览
4收藏
应活动要求
本期挑战赛将继续围绕HarmonyOS的分布式特性展开。大家可以发挥奇思妙想,构建一款互动体验应用,应用需实现最少两台设备间的互动。大家可以根据自己的需求构建应用,可以是合成1024有趣的互动应用,也可以是绘画多屏互动应用等。
重点要突出 多屏的互动要求,那也就是又分布式协同的技术在里面,而不是之前的流转
个人心得:如有错误还请大佬及时指正 .
分布式流转: 一个APP应用再设备之间互相拉起迁移 ,只在 ** 一个 !** 终端上运行.
分布式协同: 一个APP **同时 ! 在多个设备上运行 **,画面实时共享,数据实时传输.
在工程创立之后
首先有个很重要的事情那就是验权
1.这个分布式协同会用到一个权限接口 去MainAbilitySlice里面申请
分布式数据管理 ohos.permission.DISTRIBUTED_DATASYNC 允许不同设备间的数据交换。
void grantPermission() { //获取验证权限 数据交互 允许不同设备间的数据交换。
if (verifySelfPermission(DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
if (canRequestPermission(DISTRIBUTED_DATASYNC)) {
requestPermissionsFromUser(new String[] {DISTRIBUTED_DATASYNC}, PERMISSION_CODE);
}
}
}
文档链接:权限接口文档
2.权限申请
开发者需要在config.json文件中的“reqPermissions”字段中声明所需要的权限。
{
"module": {
"reqPermissions": [
{
"name": "ohos.permission.CAMERA",
"reason": "$string:permreason_camera",
"usedScene":
{
"ability": ["com.mycamera.Ability", "com.mycamera.AbilityBackground"],
"when": "always"
}
},{
...
}
]
}
}
}
{
"name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" //允许获取分布式组网内设备的状态变化。
},
{
"name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" // 允许获取分布式组网内的设备列表和设备信息。
},
{
"name": "ohos.permission.GRT_BUNDLE_INFO" //查询其他应用的信息。
},
{
"name": "ohos.permission.INTERNET" //允许使用网络socket。
}
文档链接:应用权限列表
再看页面结构
’
在resources下面的
graphic:页面样式效果调配
layoput:此demo的java UI页面布局结构
主页面入口布局代码 ability_main
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:background_element="$graphic:background_button">
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:top_margin="150px"
ohos:layout_alignment="horizontal_center"
ohos:text=" 你好鸿蒙_你画我猜"
ohos:text_size="38fp"
/>
<Image
ohos:id="$+id:imageComponent"
ohos:height="200vp"
ohos:width="1080"
ohos:top_margin="150px"
ohos:image_src="$media:HM"
/>
<Button
ohos:id="$+id:help_btn"
ohos:height="100vp"
ohos:width="300vp"
ohos:background_element="$graphic:background_button"
ohos:layout_alignment="horizontal_center"
ohos:left_padding="15vp"
ohos:right_padding="15vp"
ohos:text="进入游戏"
ohos:text_size="30vp"
ohos:top_margin="20vp">
</Button>
</DirectionalLayout>
游戏匹配页面布局 math_game
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:help_btn"
ohos:height="match_content"
ohos:width="500px"
ohos:background_element="$graphic:background_begin"
ohos:layout_alignment="horizontal_center"
ohos:left_padding="15vp"
ohos:right_padding="15vp"
ohos:text="匹配对手"
ohos:text_size="30vp"
ohos:top_margin="200vp">
</Button>
<Image
ohos:id="$+id:imageComponent"
ohos:height="200vp"
ohos:width="1080"
ohos:top_margin="100px"
ohos:image_src="$media:NHWC"
/>
</DirectionalLayout>
MainAbilitySlice
package com.huawei.codelab.slice;
import static ohos.security.SystemPermission.DISTRIBUTED_DATASYNC;
import com.huawei.codelab.ResourceTable;
import com.huawei.codelab.utils.CommonData;
import com.huawei.codelab.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.bundle.IBundleManager;
public class MainAbilitySlice extends AbilitySlice {
private static final String TAG = CommonData.TAG + MainAbilitySlice.class.getSimpleName();
private static final int PERMISSION_CODE = 10000000;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
grantPermission();
initView();
}
void grantPermission() { //获取验证权限 数据交互 允许不同设备间的数据交换。
if (verifySelfPermission(DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
if (canRequestPermission(DISTRIBUTED_DATASYNC)) {
requestPermissionsFromUser(new String[] {DISTRIBUTED_DATASYNC}, PERMISSION_CODE);
}
}
}
//
private void initView() {
findComponentById(ResourceTable.Id_help_btn).setClickedListener(new ButtonClick());
}
private void mathGame() { //数学游戏
LogUtil.info(TAG, "Click ResourceTable Id_math_game");
Intent mathGameIntent = new Intent();
Operation operationMath = new Intent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.MATH_PAGE)
.build();
mathGameIntent.setOperation(operationMath);
startAbility(mathGameIntent);
}
//进入游戏
private class ButtonClick implements Component.ClickedListener {
@Override
public void onClick(Component component) {
mathGame();
}
}
}
MathGameAbilitySlice
package com.huawei.codelab.slice;
import com.huawei.codelab.ResourceTable;
import com.huawei.codelab.devices.SelectDeviceDialog;
import com.huawei.codelab.utils.CommonData;
import com.huawei.codelab.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import java.util.ArrayList;
import java.util.List;
public class MathGameAbilitySlice extends AbilitySlice {
private static final String TAG = CommonData.TAG + MathGameAbilitySlice.class.getSimpleName();
private Button helpBtn;
private List<DeviceInfo> devices = new ArrayList<>();
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_math_game);
initView();
}
private void initView() {
if (findComponentById(ResourceTable.Id_help_btn) instanceof Button) {
helpBtn = (Button) findComponentById(ResourceTable.Id_help_btn);
}
helpBtn.setClickedListener(new ButtonClick());
}
private void getDevices() {
if (devices.size() > 0) {
devices.clear();
}
List<DeviceInfo> deviceInfos =
DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
LogUtil.info(TAG, "MathGameAbilitySlice deviceInfos size is :" + deviceInfos.size());
devices.addAll(deviceInfos);
showDevicesDialog();
}
private void showDevicesDialog() {
new SelectDeviceDialog(this, devices, deviceInfo -> {
startLocalFa(deviceInfo.getDeviceId());
startRemoteFa(deviceInfo.getDeviceId());
}).show();
}
private void startLocalFa(String deviceId) {
LogUtil.info(TAG, "startLocalFa......");
Intent intent = new Intent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID, deviceId);
intent.setParam(CommonData.KEY_IS_LOCAL, true);
Operation operation = new Intent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.DRAW_PAGE)
.build();
intent.setOperation(operation);
startAbility(intent);
}
private void startRemoteFa(String deviceId) {
LogUtil.info(TAG, "startRemoteFa......");
String localDeviceId =
KvManagerFactory.getInstance().createKvManager(new KvManagerConfig(this)).getLocalDeviceInfo().getId();
Intent intent = new Intent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID, localDeviceId);
intent.setParam(CommonData.KEY_IS_LOCAL, false);
Operation operation = new Intent.OperationBuilder().withDeviceId(deviceId)
.withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.DRAW_PAGE)
.withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
.build();
intent.setOperation(operation);
startAbility(intent);
}
private class ButtonClick implements Component.ClickedListener {
@Override
public void onClick(Component component) {
getDevices();//启动机器匹配
}
}
}
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
赞
8
收藏 4
回复
相关推荐
1024节日快乐。