当前位置: 首页> 教育> 锐评 > 黄骅贴吧足疗_网络推广岗位职责和任职要求_点金推广优化公司_100%能上热门的文案

黄骅贴吧足疗_网络推广岗位职责和任职要求_点金推广优化公司_100%能上热门的文案

时间:2025/7/11 0:59:27来源:https://blog.csdn.net/m0_68038853/article/details/146798545 浏览次数:0次
黄骅贴吧足疗_网络推广岗位职责和任职要求_点金推广优化公司_100%能上热门的文案

当在鸿蒙应用中,网络状态发生变化,我们需要实现监听网络状态的变化,做出相对应的提示,比如退出登录或者退出应用之类的操作。那么我们需要监听当前网络的状态,以下是我封装的代码提供参考:

效果:

参考代码:

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { CommonConstants } from '../constants/CommonConstants';
import { JumpTool } from '.';
import { DialogContents, H5ShowCommonDialog } from '../../view/H5CommonDialog';
import { WindowUtils } from '../constants/WindowUtils';
import { LoginTool } from './LoginTool';
import Logger from './Logger';
​
export class ListenNetworkState {private static oldNetType: string | null = nullprivate static newNetType: string | null = null
​/***  获取旧网络* @param* @returns*/static async getOldNet() {connection.getDefaultNet().then((netHandle: connection.NetHandle) => {if (netHandle.netId == 0) {// 当前没有已连接的网络时,获取的netHandler的netid为0,属于异常场景,此处可以实际情况自行添加一些处理机制。return;}connection.getNetCapabilities(netHandle, (error: BusinessError, data: connection.NetCapabilities) => {if (error) {return;}// Logger.info("Succeeded to get data: " + JSON.stringify(data));if (data.bearerTypes[0] == 0) {ListenNetworkState.oldNetType = '蜂窝网络'// promptAction.showToast({ message: '蜂窝网络' })}if (data.bearerTypes[0] == 1) {ListenNetworkState.oldNetType = 'Wi-Fi网络'// promptAction.showToast({ message: 'Wi-Fi网络' })}})}).catch((error: BusinessError) => {});}
​/*** 监听网络是否变化* @param* @returns true 变化 false 无变化*/static async listenNetwork() {let isNetworkChanged = false;await ListenNetworkState.getOldNet();let currentNet = connection.createNetConnection();currentNet.register((error) => {if (error) {// Logger.info('订阅失败' + JSON.stringify(error));} else {// Logger.info('订阅成功' + JSON.stringify(error));}});
​//网络切换时currentNet.on("netCapabilitiesChange", (data) => {if (data.netCap.bearerTypes[0] == 0) {ListenNetworkState.newNetType = '蜂窝网络';}if (data.netCap.bearerTypes[0] == 1) {ListenNetworkState.newNetType = 'Wi-Fi网络';}if (ListenNetworkState.oldNetType !== ListenNetworkState.newNetType) {if (CommonConstants.ISLOGIN) {LoginTool.logOut(false, false)
​let context: DialogContents = {title: "提示",message: '网络发生变化,已退出登录',confirms: () => {WindowUtils.getRouter().pushUrl({ url: 'pages/login/LoginPwdPage' })}}H5ShowCommonDialog(context)}ListenNetworkState.oldNetType = ListenNetworkState.newNetType}});
​//无网络切换到有网络时currentNet.on("netAvailable", (data) => {
​if (CommonConstants.ISLOGIN) {LoginTool.logOut(false, false)
​let context: DialogContents = {title: "提示",message: '网络发生变化,已退出登录',confirms: () => {WindowUtils.getRouter().pushUrl({ url: 'pages/login/LoginPwdPage' })}}H5ShowCommonDialog(context)}});}
}

关键字:黄骅贴吧足疗_网络推广岗位职责和任职要求_点金推广优化公司_100%能上热门的文案

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: