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 0000000000000000000000000000000000000000..0340f56556ba8891488b295a030afaf536ce57e3 --- /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 diff --git a/NetworkKit/entry/src/main/ets/pages/GetNetworkFamily b/NetworkKit/entry/src/main/ets/pages/GetNetworkFamily new file mode 100644 index 0000000000000000000000000000000000000000..09fae79f1481cbd1022c00ed7f86a36632bab7cb --- /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 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 0000000000000000000000000000000000000000..e5a9ae4abb147d36e1c7df4fb745b55415ed3244 --- /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 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 0000000000000000000000000000000000000000..bc6130ca989138a34cdcc729781c02a1e8749519 --- /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 diff --git a/NetworkKit/entry/src/main/module_getNetwork.json5 b/NetworkKit/entry/src/main/module_getNetwork.json5 new file mode 100644 index 0000000000000000000000000000000000000000..6222823c6600d2ba10e057212ee509840f8b61a0 --- /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