今天给大家分享一个有趣的鸿蒙分布式小游戏:你画我猜。
开发心得(如有错误还请大佬及时指正):
-
分布式流转:一个 APP 应用在设备之间互相拉起迁移,只在一个终端上运行。
- 分布式协同:一个 APP 同时在多个设备上运行,画面实时共享,数据实时传输。
在工程创立之后,首先有个很重要的事情那就是验权。
①这个分布式协同会用到一个权限接口,去 MainAbilitySlice 里面申请
分布式数据管理 ohos.permission.DISTRIBUTED_DATASYNC 允许不同设备间的数据交换。
voidgrantPermission(){//获取验证权限数据交互允许不同设备间的数据交换。
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{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:
packagecom.huawei.codelab.slice;
importstaticohos.security.SystemPermission.DISTRIBUTED_DATASYNC;
importcom.huawei.codelab.ResourceTable;
importcom.huawei.codelab.utils.CommonData;
importcom.huawei.codelab.utils.LogUtil;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.aafwk.content.Operation;
importohos.agp.components.Component;
importohos.bundle.IBundleManager;
publicclassMainAbilitySliceextendsAbilitySlice{
privatestaticfinalStringTAG=CommonData.TAG+MainAbilitySlice.class.getSimpleName();
privatestaticfinalintPERMISSION_CODE=10000000;
@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
grantPermission();
initView();
}
voidgrantPermission(){//获取验证权限数据交互允许不同设备间的数据交换。
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},PERMISSION_CODE);
}
}
}
//
privatevoidinitView(){
findComponentById(ResourceTable.Id_help_btn).setClickedListener(newButtonClick());
}
privatevoidmathGame(){//数学游戏
LogUtil.info(TAG,"ClickResourceTableId_math_game");
IntentmathGameIntent=newIntent();
OperationoperationMath=newIntent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.MATH_PAGE)
.build();
mathGameIntent.setOperation(operationMath);
startAbility(mathGameIntent);
}
//进入游戏
privateclassButtonClickimplementsComponent.ClickedListener{
@Override
publicvoidonClick(Componentcomponent){
mathGame();
}
}
}
MathGameAbilitySlice:
packagecom.huawei.codelab.slice;
importcom.huawei.codelab.ResourceTable;
importcom.huawei.codelab.devices.SelectDeviceDialog;
importcom.huawei.codelab.utils.CommonData;
importcom.huawei.codelab.utils.LogUtil;
importohos.aafwk.ability.AbilitySlice;
importohos.aafwk.content.Intent;
importohos.aafwk.content.Operation;
importohos.agp.components.Button;
importohos.agp.components.Component;
importohos.data.distributed.common.KvManagerConfig;
importohos.data.distributed.common.KvManagerFactory;
importohos.distributedschedule.interwork.DeviceInfo;
importohos.distributedschedule.interwork.DeviceManager;
importjava.util.ArrayList;
importjava.util.List;
publicclassMathGameAbilitySliceextendsAbilitySlice{
privatestaticfinalStringTAG=CommonData.TAG+MathGameAbilitySlice.class.getSimpleName();
privateButtonhelpBtn;
privateListdevices=newArrayList<>();
@Override
publicvoidonStart(Intentintent){
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_math_game);
initView();
}
privatevoidinitView(){
if(findComponentById(ResourceTable.Id_help_btn)instanceofButton){
helpBtn=(Button)findComponentById(ResourceTable.Id_help_btn);
}
helpBtn.setClickedListener(newButtonClick());
}
privatevoidgetDevices(){
if(devices.size()>0){
devices.clear();
}
ListdeviceInfos=
DeviceManager.getDeviceList(ohos.distributedschedule.interwork.DeviceInfo.FLAG_GET_ONLINE_DEVICE);
LogUtil.info(TAG,"MathGameAbilitySlicedeviceInfossizeis:"+deviceInfos.size());
devices.addAll(deviceInfos);
showDevicesDialog();
}
privatevoidshowDevicesDialog(){
newSelectDeviceDialog(this,devices,deviceInfo->{
startLocalFa(deviceInfo.getDeviceId());
startRemoteFa(deviceInfo.getDeviceId());
}).show();
}
privatevoidstartLocalFa(StringdeviceId){
LogUtil.info(TAG,"startLocalFa......");
Intentintent=newIntent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID,deviceId);
intent.setParam(CommonData.KEY_IS_LOCAL,true);
Operationoperation=newIntent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(CommonData.ABILITY_MAIN)
.withAction(CommonData.DRAW_PAGE)
.build();
intent.setOperation(operation);
startAbility(intent);
}
privatevoidstartRemoteFa(StringdeviceId){
LogUtil.info(TAG,"startRemoteFa......");
StringlocalDeviceId=
KvManagerFactory.getInstance().createKvManager(newKvManagerConfig(this)).getLocalDeviceInfo().getId();
Intentintent=newIntent();
intent.setParam(CommonData.KEY_REMOTE_DEVICEID,localDeviceId);
intent.setParam(CommonData.KEY_IS_LOCAL,false);
Operationoperation=newIntent.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);
}
privateclassButtonClickimplementsComponent.ClickedListener{
@Override
publicvoidonClick(Componentcomponent){
getDevices();//启动机器匹配
}
}
}
责任编辑:haq
-
鸿蒙系统
+关注
关注
183文章
2634浏览量
66138 -
HarmonyOS
+关注
关注
79文章
1966浏览量
29955
原文标题:鸿蒙版你画我猜,请接招!
文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论