今天給大家分享一個有趣的鴻蒙分布式小游戲:你畫我猜。
開發(fā)心得(如有錯誤還請大佬及時指正):
-
分布式流轉(zhuǎn):一個 APP 應(yīng)用在設(shè)備之間互相拉起遷移,只在一個終端上運行。
- 分布式協(xié)同:一個 APP 同時在多個設(shè)備上運行,畫面實時共享,數(shù)據(jù)實時傳輸。
在工程創(chuàng)立之后,首先有個很重要的事情那就是驗權(quán)。
①這個分布式協(xié)同會用到一個權(quán)限接口,去 MainAbilitySlice 里面申請
分布式數(shù)據(jù)管理 ohos.permission.DISTRIBUTED_DATASYNC 允許不同設(shè)備間的數(shù)據(jù)交換。
voidgrantPermission(){//獲取驗證權(quán)限數(shù)據(jù)交互允許不同設(shè)備間的數(shù)據(jù)交換。
if(verifySelfPermission(DISTRIBUTED_DATASYNC)!=IBundleManager.PERMISSION_GRANTED){
if(canRequestPermission(DISTRIBUTED_DATASYNC)){
requestPermissionsFromUser(newString[]{DISTRIBUTED_DATASYNC},PERMISSION_CODE);
}
}
}
權(quán)限接口文檔鏈接:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-available-0000001051089272
②權(quán)限申請
開發(fā)者需要在 config.json 文件中的“reqPermissions”字段中聲明所需要的權(quán)限。
{
"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"//允許獲取分布式組網(wǎng)內(nèi)設(shè)備的狀態(tài)變化。
},
{
"name":"ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"//允許獲取分布式組網(wǎng)內(nèi)的設(shè)備列表和設(shè)備信息。
},
{
"name":"ohos.permission.GRT_BUNDLE_INFO"//查詢其他應(yīng)用的信息。
},
{
"name":"ohos.permission.INTERNET"//允許使用網(wǎng)絡(luò)socket。
}
應(yīng)用權(quán)限列表文檔鏈接:
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/security-permissions-guidelines-0000000000029886
再看頁面結(jié)構(gòu):
在 resources 下面的:-
graphic:頁面樣式效果調(diào)配
-
layoput:此 demo 的 java UI 頁面布局結(jié)構(gòu)
主頁面入口布局代碼 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="進(jìn)入游戲"
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(){//獲取驗證權(quán)限數(shù)據(jù)交互允許不同設(shè)備間的數(shù)據(jù)交換。
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(){//數(shù)學(xué)游戲
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);
}
//進(jìn)入游戲
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();//啟動機(jī)器匹配
}
}
}
責(zé)任編輯:haq
-
鴻蒙系統(tǒng)
+關(guān)注
關(guān)注
183文章
2634瀏覽量
66220 -
HarmonyOS
+關(guān)注
關(guān)注
79文章
1967瀏覽量
30019
原文標(biāo)題:鴻蒙版你畫我猜,請接招!
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論