HarmonyOS鸿蒙学习笔记(23)监听Wifi状态变化

news/2024/10/23 9:25:39/

监听Wifi状态变化

  • 前言
  • 创建接收状态变化的Bean对象
  • 创建订阅者和订阅事件
  • 参考资料:

前言

本篇博文通过动态订阅公共事件来说明怎么使用HarmonyOS监听Wifi状态的变化。关于动态订阅公共事件的概念,官网有详细说明,再次就不在赘述。博文相关项目源码地址传送门。公共事件的监听需要通过订阅和注销两步。
在这里插入图片描述

创建接收状态变化的Bean对象

该对象主要用来存储Wifi当前的状态,比如是否已经链接、是否断开等。同时该Bean对象还定义了一个subscriber用来保持订阅者,用来执行订阅和注销


export class CommonEventBean {//省略部分代码/*** The state of common events.*/state: Resource = $r('app.string.event_init_state');//保存订阅者对象subscriber: any = null;
}

创建订阅者和订阅事件

监听Wifi变化,需要先调用CommonEventManager.createSubscriber创建订阅者,保存返回的订阅者对象subscriber,用于执行后续的订阅、退订等操作。下面看看就看具体怎么来监听Wifi变化的:

 /**@param commonEventItem 保存状态的Bean对象@*/subscribe(commonEventItem: CommonEventBean, callback: Function): void {let toastMsg: Resource;let commonEvent = commonEventItem;//创建需要订阅的事件,此处为CONN_STATEconst subscribeInfo = {events: [CommonConstants.CONN_STATE]};//创建订阅者CommonEventManager.createSubscriber(subscribeInfo, (err, subscriber) => {if (err) {toastMsg = $r('app.string.subscribe_fail');//创建订阅失败:执行回调,刷新相关UIcallback(commonEvent, toastMsg);return;}// 创建订阅者失败:执行回调,刷新相关UIif (subscriber === null) {toastMsg = $r('app.string.need_subscriber');callback(commonEvent, toastMsg);return;}//保存订阅者,用来后面的注销操作commonEvent.subscriber = subscriber;//通过订阅者subscriber 执行订阅 //订阅回调函数返回的data内包含了公共事件的名称、发布者携带的数据等信息CommonEventManager.subscribe(subscriber, (err, data) => {if (err) {//订阅失败:执行回调,刷新相关UItoastMsg = $r('app.string.subscribe_fail');callback(commonEvent, toastMsg);return;}let connState: string | undefined = data?.data;if (connState === undefined) {return;}//变量当前Wifi状态switch (connState) {case WifiState.CONNECTING:commonEvent.state = '连接中';break;case WifiState.DISCONNECTED:commonEvent.state = '已断开';break;case WifiState.DISCONNECTING:commonEvent.state = '正在断开';break;case WifiState.UNKNOWN_STATE:commonEvent.state = '未知状态';break;case WifiState.AP_CONNECTED:commonEvent.state = '已连接';break;default:break;}//执行回调,刷新相关UIcallback(commonEvent);})toastMsg = $r('app.string.subscribe_success');//执行回调,刷新相关UIcallback(commonEvent, toastMsg);})}

参考资料:

系统公共事件(ArkTS)
动态订阅公共事件
源码地址


http://www.ppmy.cn/news/1332959.html

相关文章

Unity Text超框 文字滚动循环显示

Unity Text超框 文字滚动循环显示 //container Text using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI;public class AutoScrollText : MonoBehaviour {private Text[] _texts new Text[…

SIP INVITE method

在RFC 3261定义了SIP:INVITE,以下是具体内容。 当UA客户端希望发起session,例如voice call 或video call时,UAC就可以发送INVITE request。INVITE request会要求服务器建立session,然后该请求由代理转发,最终到达一个或多个可能接受邀请的UAS。 UAS 可以通过发送 2xx res…

C#-前后端分离连接mysql数据库封装接口

C#是世界上最好的语言 新建项目 如下图所示选择框红的项目 然后新建 文件夹 Common 并新建类文件 名字任意 文件内容如下 因为要连接的是mysql数据库 所以需要安装 MySql.Data.MySqlClient 依赖; using MySql.Data.MySqlClient; using System.Data;namespace WebApplication1.…

微信小程序(十一)表单组件(进阶)

注释很详细,直接上代码 上一篇 新增内容:(涉及内容较多,建议细看源码) 1.radio-group的使用与数据处理 2.checkbox-group的使用与数据处理 3.picker的使用与数据同步处理(此处示范了地域与日期) 源码: form…

训练YOLOv5模型(云端GPU)

Colab 选择GPU 查看配置 ! nvidia-smi上传压缩包并解压 压缩包 -> 解压的文件 !unzip /content/yolov5-5.0.zip -d/content/yolov5进入目标文件夹下 %cd /content/yolov5/yolov5-5.0安装所需包package !pip install -r requirements.txt添加插件-Tensorboard 失败的话…

搭建k8s集群实战(二)安装keepalived和haproxy

keepalived介绍: 是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障 Keepalived作用: 为haproxy提供vip(10.208.1.190)在三个haproxy实例之间提供主备,降低当其中一个haproxy失效的时对服务的影响。 1、yum安装Keepalived【三个master…

Unity UIBasePanel 简单的ui基类

简单的ui基类 UIBasePanel.cs using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic;namespace MYTOOL.UI {public class UIBasePanel : MonoBehaviour{//通过里式转换原则 来存储所有的控件private readonly Dictio…

mysql学习打卡day17

今日成果: insert into products (name,quantity_in_stock,unit_price) values(t1,10,1.1),(tom,20,1.23),(t2,11,12.2); -- 一次插入多条数据 -- 字符串和日期需要加引号 -- PK代表主键记录的唯一标识 -- NN表示非空 -- AI表示自动增长 感谢各位读者查阅&#x…