今天给大家分享一个有趣的鸿蒙分布式小游戏:你画我猜。
分布式流转:一个 APP 应用在设备之间互相拉起迁移,只在一个终端上运行。
在工程创立之后,首先有个很重要的事情那就是验权。
①这个分布式协同会用到一个权限接口,去 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);
}
}
}
权限接口文档链接:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-available-0000001051089272
②权限申请
开发者需要在 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。
}
应用权限列表文档链接:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-guidelines-0000000000029886
再看页面结构:
在 resources 下面的:graphic:页面样式效果调配
layoput:此 demo 的 java UI 页面布局结构
主页面入口布局代码 ability_main:
<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:
<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 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 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();//启动机器匹配
}
}
}
责任编辑:haq
全部0条评论
快来发表一下你的评论吧 !