代码拉取完成,页面将自动刷新
本示例提供了通过 Network Kit 获取并监听网络状态的能力,可以在网络变化时执行应用预设的逻辑。
点击 “订阅网络事件” 按钮,监听到有可用网络时,将相关信息输出在下方。
点击 “查看网络状态” 按钮,会获取当前网络信息,并输出在下方。
需要在模块 module.json5 中声明以下权限。
先通过 netConnection.register() 注册事件监听。之后再通过 netConnection.on() 注册相应事件的回调。
registerNetworkEvent(): void {
this.netConnection.register((error: BusinessError) => {
if (error) {
this.eventOutput = `netConnection register error: ${JSON.stringify(error)}`;
hilog.error(0x00FF, 'NetworkAwareness', this.eventOutput);
}
});
// 网络可用
this.netConnection.on('netAvailable', (data: connection.NetHandle) => {
this.eventOutput = `network available: ${JSON.stringify(data)}`;
hilog.info(0x00FF, 'NetworkAwareness', this.eventOutput);
if (this.lastAvailableNetworkId === COMMON_INVALID_VAL) {
this.lastAvailableNetworkId = data.netId;
} else if (this.lastAvailableNetworkId === data.netId) {
// 当前网络和上次连接的可用网络为同一网络
} else {
// 当前网络和上次连接的可用网络为不同网络
}
});
// 网络丢失 (例如网络断开)
this.netConnection.on('netLost', (data: connection.NetHandle) => {
this.eventOutput = `network lost: ${JSON.stringify(data)}`;
hilog.info(0x00FF, 'NetworkAwareness', this.eventOutput);
});
// 网络不可用 (例如 WIFI 图标上有感叹号)
this.netConnection.on('netUnavailable', () => {
this.eventOutput = `network unavailable, id is ${this.lastAvailableNetworkId}`;
hilog.info(0x00FF, 'NetworkAwareness', this.eventOutput);
});
}
先通过 connection.getDefaultNet() 获取 netHandle。再通过 connection.getNetCapabilities() 获取 netHandle 对应的网络的能力信息。
getNetworkStatus(): void {
connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
if (netHandle.netId === 0) {
// 当前没有已连接的网络时,获取的 netHandler 的 netid 为 0
this.statusOutput = `No network connected`;
hilog.info(0x00FF, 'NetworkAwareness', this.statusOutput);
return;
}
connection.getNetCapabilities(netHandle, (error: BusinessError, netCapabilities: connection.NetCapabilities) => {
if (error) {
this.statusOutput = `Failed to get net capabilities. Code:${error.code}, message:${error.message}`;
hilog.error(0x00FF, 'NetworkAwareness', this.statusOutput);
return;
}
let currentNetInfoTmp =
`上/下行带宽:${netCapabilities.linkUpBandwidthKbps}/${netCapabilities.linkDownBandwidthKbps} kb/s\n`;
let netBearType: connection.NetBearType = netCapabilities.bearerTypes[0];
currentNetInfoTmp += `网络类型:${getNetBearType(netBearType)}\n`;
let properties: connection.ConnectionProperties = connection.getConnectionPropertiesSync(netHandle);
currentNetInfoTmp += `网卡名称:${properties.interfaceName}\n`;
currentNetInfoTmp += `所属域:${properties.domains}\n`;
currentNetInfoTmp += `最大传输单元:${properties.mtu}\n`;
this.statusOutput = currentNetInfoTmp;
});
}).catch((error: BusinessError) => {
this.statusOutput = `netHandle get error: ${JSON.stringify(error)}`;
hilog.error(0x00FF, 'NetworkAwareness', this.statusOutput);
});
}
entry/src/main/ets/
|---entryability
|---pages
|---Index.ets // 示例展示页面
|---utils
|---NetworkUtil.ets // 网络处理类
无
修改内容 | 时间 |
---|---|
第一次提交 | 2025.03.10 |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。