赞
踩
发布会现场
鸿蒙系统架构
对Ability(能力)的解释
我们对比一下鸿蒙和安卓的架构
安卓系统
鸿蒙系统
对比
在线体验:https://playground.harmonyos.com/
开发环境搭建(DevEco Studio)
鸿蒙js
https://github.com/quickjs-zh/QuickJS
目录结构
html
<element name="fragment_main" src="../../common/fragment_main/fragment_main.hml"></element> <div class="container"> <!-- top tool bar --> <div class="top-tool-bar"> <image class="toolbar-image1" src="{{ images_resource.image_add }}" onclick="onClick"></image> <image class="toolbar-image2" src="{{ images_resource.image_add }}" onclick="onClick"></image> <image class="toolbar-image3" src="{{ images_resource.image_more }}" onclick="onClick2"></image> </div> <!--bottom tool Bar--> <tabs class="tabs" index="0" vertical="false" onchange="change"> <tab-bar class="bottomBar-wrapper" mode="fixed"> <div class="bottomBar-item-wrapper"> <image class="bottomBarItem-image" src="{{ images_resource.image_icon }}"></image> <text class="bottomBarItem-text">{{ $t('strings.tab_name') }}</text> </div> <div class="bottomBar-item-wrapper"> <image class="bottomBarItem-image" src="{{ images_resource.image_icon }}"></image> <text class="bottomBarItem-text">{{ $t('strings.tab_name') }}</text> </div> <div class="bottomBar-item-wrapper"> <image class="bottomBarItem-image" src="{{ images_resource.image_icon }}"></image> <text class="bottomBarItem-text">{{ $t('strings.tab_name') }}</text> </div> <div class="bottomBar-item-wrapper"> <image class="bottomBarItem-image" src="{{ images_resource.image_icon }}"></image> <text class="bottomBarItem-text">{{ $t('strings.tab_name') }}</text> </div> <div class="bottomBar-item-wrapper"> <image class="bottomBarItem-image" src="{{ images_resource.image_icon }}"></image> <text class="bottomBarItem-text">{{ $t('strings.tab_name') }}</text> </div> </tab-bar> <tab-content class="tabContent" scrollable="true"> <div class="item-content"> <fragment_main></fragment_main> </div> <div class="item-content"> <text class="item-title">{{ $t('strings.second_page') }}</text> </div> <div class="item-content"> <text class="item-title">{{ $t('strings.third_page') }}</text> </div> <div class="item-content"> <text class="item-title">{{ $t('strings.fourth_page') }}</text> </div> <div class="item-content"> <text class="item-title">{{ $t('strings.fifth_page') }}</text> </div> </tab-content> </tabs> <div class="fragment_main_wearable"> <fragment_main id="id_fragment_main"></fragment_main> </div> </div>
js
import prompt from '@system.prompt'; import device from '@system.device'; import mediaquery from '@system.mediaquery'; const TAG = '[index]'; var mMediaQueryList; var context; // js call java // abilityType: 0-Ability; 1-Internal Ability const ABILITY_TYPE_EXTERNAL = 0; const ABILITY_TYPE_INTERNAL = 1; // syncOption(Optional, default sync): 0-Sync; 1-Async const ACTION_SYNC = 0; const ACTION_ASYNC = 1; const ACTION_MESSAGE_CODE = 1001; var wearableMediaListener = function (e) { if (e.matches) { // do something console.log(TAG + "Success Media Listen"); context.initial_value = 4; context.$child('id_fragment_main').initial_index_value = 0; context.$child('id_fragment_main').list_data.forEach(element => { element.item_icon = context.images_resource_dark_mode.image_icon; element.item_right_arrow = context.images_resource_dark_mode.image_right_arrow; }); } }; var getDeviceInfo = function () { var res = ''; device.getInfo({ success: function (data) { console.log(TAG + 'Success device obtained. screenShape=' + data.screenShape); this.res = data.screenShape; }, fail: function (data, code) { console.log(TAG + 'Failed to obtain device. Error code=' + code + '; Error information: ' + data); }, }); return res; }; export default { data: { images_resource: { "image_icon": "common/images/ic.png", "image_add": "common/images/add64.png", "image_more": "common/images/more.png", "image_right_arrow": "common/images/right_arrow.png" }, images_resource_dark_mode: { "image_icon": "common/images/ic_dark_mode.png", "image_add": "common/images/add64.svg", "image_more": "common/images/more.svg", "image_right_arrow": "common/images/right_arrow_dark_mode.png" }, initial_value: 0 }, onInit() { console.log(TAG + 'onInit'); context = this; this.mMediaQueryList = mediaquery.matchMedia("screen and (device-type: wearable)"); this.mMediaQueryList.addListener(wearableMediaListener); console.info(TAG + "java js" + this.getSystemColorModeByJava()); // async call and return null }, onReady() { console.log(TAG + 'onReady'); console.log(TAG + getDeviceInfo()); // getDeviceInfo after Init }, onShow() { console.log(TAG + 'onShow'); }, onDestroy() { console.log(TAG + 'onDestroy'); mMediaQueryList.removeListener(wearableMediaListener); }, onClick() { prompt.showToast({ message: "add", duration: 3000, }); }, onClick2() { prompt.showToast({ message: "more", duration: 3000, }); }, getSystemColorModeByJava: async function() { var actionData = {}; actionData.firstNum = 123; actionData.secondNum = 465; var action = {}; action.bundleName = 'com.example.myapplication'; action.abilityName = 'com.example.myapplication.ServiceAbilityForJS'; action.messageCode = ACTION_MESSAGE_CODE; action.data = actionData; action.abilityType = ABILITY_TYPE_EXTERNAL; action.syncOption = ACTION_SYNC; var result = await FeatureAbility.callAbility(action); var ret = JSON.parse(result); if (ret.code == 0) { console.info('result is:' + JSON.stringify(ret.abilityResult)); } else { console.error('error code:' + JSON.stringify(ret.code)); } if (ret.getColorMode == 0) { console.info(TAG + ret.getColorMode); // get system color mode and do something this.images_resource = this.images_resource_dark_mode; } } }
css
.container { display: flex; flex-direction: column; justify-content: flex-start; align-items: flex-start; width: 100%; left: 0px; top: 0px; } .top-tool-bar { display: flex; flex-direction: row; justify-content: flex-end; align-items: center; width: 100%; height: 56px; padding-left: 24px; padding-right: 24px; } .toolbar-image1 { width: 24px; height: 24px; margin-right: 40px; opacity: 0.9; } .toolbar-image2 { width: 24px; height: 24px; margin-right: 40px; opacity: 0.9; } .toolbar-image3 { width: 24px; height: 24px; opacity: 0.9; } .tabs { width: 100%; } .bottomBar-wrapper { width: 100%; height: 56px; flex-direction: row; justify-content: flex-start; align-items: center; position: fixed; bottom: 0px; } .bottomBar-item-wrapper { width: 100%; height: 100%; flex-direction: column; justify-content: center; align-items: center; margin-top: 0px; margin-bottom: 8px; } .bottomBarItem-image { height: 24px; width: 24px; } .bottomBarItem-text { font-size: 14px; opacity: 0.9; margin-top: 2px; } .tabContent { width: 100%; padding-bottom: 56px; } .item-content { display: flex; flex-direction: column; justify-content: flex-start; align-items: flex-start; width: 100%; } .item-title { width: 100%; height: 100%; font-size: 18px; text-align: center; } /* wearable */ @media screen and (device-type: wearable) { .top-tool-bar { display: none; } .bottomBar-wrapper { display: none; } } /* phone */ @media screen and (device-type: phone) { .fragment_main_wearable { display: none; } } /* tablet */ @media screen and (device-type: tablet) { .fragment_main_wearable { display: none; } } /* tv */ @media screen and (device-type: tv) { .fragment_main_wearable { display: none; } .item-title { color: black; } .top-tool-bar { display: none; } .bottomBar-wrapper { display: none; } }
运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。