From 2fe917d9ab75ede9f123c0f3ec520ffce6381232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 01:09:46 +0000 Subject: [PATCH 1/6] =?UTF-8?q?add=20ArkUI/entry/src/main/ets/pages/TouchE?= =?UTF-8?q?xample.ets.=20=E6=96=B0=E5=A2=9EFAQ=E5=90=8C=E6=BA=90=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../entry/src/main/ets/pages/TouchExample.ets | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ArkUI/entry/src/main/ets/pages/TouchExample.ets diff --git a/ArkUI/entry/src/main/ets/pages/TouchExample.ets b/ArkUI/entry/src/main/ets/pages/TouchExample.ets new file mode 100644 index 0000000..0340f56 --- /dev/null +++ b/ArkUI/entry/src/main/ets/pages/TouchExample.ets @@ -0,0 +1,52 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:在子容器的onTouch中调用stopPropagation,为什么无法阻止外层容器的onClick事件 +*/ + +// [Start stopPropagation] +// xxx.ets +@Entry +@Component +struct TouchExample { + @State text: string = ''; + @State eventType: string = ''; + + build() { + Column() { + Button('Touch') + .height(40) + .width(100) + .onTouch((event?: TouchEvent) => { + console.info("child onTouch"); + event?.stopPropagation(); + }) + Text(this.text) + } + .onTouch((event?: TouchEvent) => { + // Since the child component called the stopPropagation interface, the onTouch event of the parent component cannot be triggered + console.info("father onTouch"); + }) + .onClick(() => { + // The child component called the stopPropagation interface but was unable to prevent the onClick event from being triggered. The parent component's onClick event was triggered as expected + console.info("father onClick"); + }) + .width('100%') + .padding(30) + } +} + +// [End stopPropagation] \ No newline at end of file -- Gitee From e4af8fad0967d7981a9a2e548c572f83ecdbebe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 07:02:56 +0000 Subject: [PATCH 2/6] add NetworkKit/entry/src/main/ets/pages/GetNetworkFamily. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../entry/src/main/ets/pages/GetNetworkFamily | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 NetworkKit/entry/src/main/ets/pages/GetNetworkFamily diff --git a/NetworkKit/entry/src/main/ets/pages/GetNetworkFamily b/NetworkKit/entry/src/main/ets/pages/GetNetworkFamily new file mode 100644 index 0000000..09fae79 --- /dev/null +++ b/NetworkKit/entry/src/main/ets/pages/GetNetworkFamily @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:如何判断当前网络的IP地址版本是多少 +*/ + +// [Start getNetwork] +import { connection } from '@kit.NetworkKit'; + +@Entry +@Component +struct Index { + getNetworkFamily() { + try { + let netHandle = connection.getDefaultNetSync(); + let connectionProperties = connection.getConnectionPropertiesSync(netHandle); + if (connectionProperties !== undefined) { + let linkAddressesArray = connectionProperties.linkAddresses; + if (linkAddressesArray !== undefined && linkAddressesArray instanceof Array && linkAddressesArray.length > 0) { + for (let i = 0; i < linkAddressesArray.length; i++) { + let address: connection.NetAddress = linkAddressesArray[i].address; + if (address !== undefined) { + console.info("Succeeded to get address: " + JSON.stringify(address)) + if (address.family === 1) { + console.info('Current network IP address version is ipv4') + } else if (address.family === 2) { + console.info('Current network IP address version is ipv6') + } + } + } + } + } + } catch (e) { + console.error(`Get exception: ${e}`); + } + } + + build() { + Column({ space: 10 }) { + Button('获取当前网络IP地址版本') + .onClick(() => { + this.getNetworkFamily(); + }) + } + .alignItems(HorizontalAlign.Center) + .height('100%') + .width('100%') + } +} +// [End getNetwork] \ No newline at end of file -- Gitee From 78649889374d47788f08e3fb374959c2e2693fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 07:09:07 +0000 Subject: [PATCH 3/6] add NetworkKit/entry/src/main/ets/pages/WifiManager.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../entry/src/main/ets/pages/WifiManager.ets | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 NetworkKit/entry/src/main/ets/pages/WifiManager.ets diff --git a/NetworkKit/entry/src/main/ets/pages/WifiManager.ets b/NetworkKit/entry/src/main/ets/pages/WifiManager.ets new file mode 100644 index 0000000..bc6130c --- /dev/null +++ b/NetworkKit/entry/src/main/ets/pages/WifiManager.ets @@ -0,0 +1,128 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:多种网络同时连接时如何优先使用wifi网络 +*/ + +// [Start getNetwork] +// WifiManager.ets +import { BusinessError } from '@kit.BasicServicesKit'; +import { connection } from '@kit.NetworkKit'; + +export class WifiManager { + private static instance?: WifiManager; + + /** + * Get singleton + * + * @returns Singleton object + */ + public static getInstance(): WifiManager { + if (!WifiManager.instance) { + WifiManager.instance = new WifiManager(); + } + return WifiManager.instance; + } + + /** + * Start listening for network changes (Wi-Fi network / Bluetooth network / cellular data) + */ + public startListenNetChange(): void { + console.info("registerNetListener"); + let netConnectionWifi = connection.createNetConnection({ + netCapabilities: { + bearerTypes: [connection.NetBearType.BEARER_WIFI] + } + }); + netConnectionWifi.register((error: BusinessError) => { + if (error) { + console.error(`register error: ${error.code}`); + } + }); + netConnectionWifi.on('netAvailable', () => { + console.info("netConnectionWifi netAvailable"); + this.bindWifiWhenConnected(); + }); + netConnectionWifi.on('netLost', () => { + console.info("Wifi netLost"); + this.bindWifiWhenConnected(); + }); + let netConnectionBluetooth = connection.createNetConnection({ + netCapabilities: { + bearerTypes: [connection.NetBearType.BEARER_BLUETOOTH] + } + }); + netConnectionBluetooth.register((error: BusinessError) => { + if (error) { + console.error(`register error: ${error.code}`); + } + }); + netConnectionBluetooth.on('netAvailable', () => { + console.info('netConnectionBluetooth netAvailable'); + this.bindWifiWhenConnected(); + }); + netConnectionBluetooth.on('netLost', () => { + console.info('Bluetooth netLost'); + this.bindWifiWhenConnected(); + }); + let netConnectionCellular = connection.createNetConnection({ + netCapabilities: { + bearerTypes: [connection.NetBearType.BEARER_CELLULAR] + } + }); + netConnectionCellular.register((error: BusinessError) => { + if (error) { + console.error(`register error: ${error.message}`); + } + }); + netConnectionCellular.on('netAvailable', () => { + console.info('netConnectionCellular netAvailable'); + this.bindWifiWhenConnected(); + }); + netConnectionCellular.on('netLost', () => { + console.info('Cellular netLost'); + this.bindWifiWhenConnected(); + }); + } + + /** + * Connect the App to the Wi-Fi network asynchronously + */ + private async bindWifiWhenConnected(): Promise { + await connection.setAppNet(connection.getDefaultNetSync()).then(() => { + console.info('setAppNet default success') + }); + connection.getAllNets().then((data: connection.NetHandle[]) => { + data.forEach(net => { + connection.getNetCapabilities(net).then((data: connection.NetCapabilities) => { + if (data.bearerTypes.length > 0 && data.bearerTypes[0] === connection.NetBearType.BEARER_WIFI) { + connection.setAppNet(net).then(() => { + console.info('setAppNet wifi success'); + return; + }).catch((error: Error) => { + console.error(`setAppNet wifi failed, error = ${error.message}`); + }) + } + }).catch((error: Error) => { + console.error(`getNetCapabilities error = ${error.message}`); + }); + }) + }).catch((error: Error) => { + console.error(`getAllNets error = ${error.message}`); + }); + } +} +// [End getNetwork] \ No newline at end of file -- Gitee From 3cfcb0d816111e07016a1b26f0eb1d932b8aa531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 07:10:30 +0000 Subject: [PATCH 4/6] add NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../main/ets/pages/StartListenNetChange.ets | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets diff --git a/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets b/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets new file mode 100644 index 0000000..1c9153d --- /dev/null +++ b/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets @@ -0,0 +1,25 @@ +/* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* +* FAQ:多种网络同时连接时如何优先使用wifi网络 +*/ + +// [Start startListenNetChange] +import { WifiManager } from './WifiManager' + +// Register for change monitoring +WifiManager.getInstance().startListenNetChange(); +// [End startListenNetChange \ No newline at end of file -- Gitee From 059539d9807345b6e1fb901863724e0c70150b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 07:11:40 +0000 Subject: [PATCH 5/6] update NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets b/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets index 1c9153d..e5a9ae4 100644 --- a/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets +++ b/NetworkKit/entry/src/main/ets/pages/StartListenNetChange.ets @@ -22,4 +22,4 @@ import { WifiManager } from './WifiManager' // Register for change monitoring WifiManager.getInstance().startListenNetChange(); -// [End startListenNetChange \ No newline at end of file +// [End startListenNetChange] \ No newline at end of file -- Gitee From 9599520cea2cd471e1e3ed244c5fc384697af3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Thu, 7 Aug 2025 07:17:57 +0000 Subject: [PATCH 6/6] add NetworkKit/entry/src/main/module_getNetwork.json5. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨梦龙 --- .../entry/src/main/module_getNetwork.json5 | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 NetworkKit/entry/src/main/module_getNetwork.json5 diff --git a/NetworkKit/entry/src/main/module_getNetwork.json5 b/NetworkKit/entry/src/main/module_getNetwork.json5 new file mode 100644 index 0000000..6222823 --- /dev/null +++ b/NetworkKit/entry/src/main/module_getNetwork.json5 @@ -0,0 +1,78 @@ +// [Start internet] +{ + "module": { + // [StartExclude internet] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude internet] + "requestPermissions":[ + { + "name": "ohos.permission.INTERNET", + "reason": "$string:internet_reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when": "inuse" + } + }, + { + "name" : "ohos.permission.GET_NETWORK_INFO", + "reason": "$string:internet_reason", + "usedScene": { + "abilities": [ + "EntryAbility" + ], + "when":"inuse" + } + }, + ] + } +} +// [End internet] \ No newline at end of file -- Gitee