事件订阅
介绍
本示例通过@ohos.telephony.observer等接口订阅网络状态、信号状态、蜂窝数据、sim状态等事件,并获取状态变化返回的结果。
效果预览
使用说明
1.打开应用,开启所有订阅事件开关。
2.开关一次移动网络,触发网络状态变化。
3.插拔一次sim卡,触发sim卡状态变化和信号状态变化。
4.点击查看详情按钮,跳转详情页,显示监听到的数据结果。
具体实现
- 该示例使用NetworkState方法获取网络注册状态,SignalInformation方法获取网络信号强度信息,RadioTechnology方法获取无线接入技术,DataConnectState方法描述蜂窝数据链路连接状态,DataFlowType方法描述蜂窝数据流类型,SimStateData方法获取SIM卡类型和状态等方法提供订阅管理功能。
- 源码参考:[DetailData.ts]
/*
* Copyright (c) 2022 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.
*/
import { radio,call,data,observer } from '@kit.TelephonyKit'
export default class DetailData {
//网络注册状态
public networkState: radio.NetworkState
//网络信号强度信息
public signalInformation: Array<radio.SignalInformation>
//通话状态
public callState?: call.CallState
//电话号码
public callNumber?: string
//蜂窝数据链路连接状态
public dataConnectState?: data.DataConnectState
//无线接入技术
public ratType?: radio.RadioTechnology
//蜂窝数据流类型
public dataFlowType: data.DataFlowType
//SIM卡类型和状态
public simStateData: observer.SimStateData
constructor() {
this.networkState = undefined
this.signalInformation = undefined
this.callState = undefined
this.callNumber = undefined
this.dataConnectState = undefined
this.ratType = undefined
this.dataFlowType = undefined
this.simStateData = undefined
}
}
- 源码参考[Index.ets]
/*
* Copyright (c) 2022-2023 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.
*/
import Logger from '../modle/Logger'
import { router } from '@kit.ArkUI'
import DetailData from '../modle/DetailData'
import { data,radio,observer } from '@kit.TelephonyKit'
const TAG: string = 'Index'
let detailData: DetailData = new DetailData()
class Value {
state: data.DataConnectState = data.DataConnectState.DATA_STATE_UNKNOWN
network: radio.RadioTechnology = radio.RadioTechnology.RADIO_TECHNOLOGY_UNKNOWN
}
@Entry
@Component
struct Index {
@State subscribes: Array<Resource> = [
$r('app.string.network_state_change'),
$r('app.string.signal_info_change'),
$r('app.string.cellular_data_connection_state_change'),
$r('app.string.cellular_data_flow_change'),
$r('app.string.sim_state_change')
]
networkStateChangeCallback = (data: observer.NetworkState) => {
Logger.info(TAG, `on networkStateChange, data: ${JSON.stringify(data)}`)
detailData.networkState = data as observer.NetworkState
}
signalInfoChangeCallback = (data: Array<radio.SignalInformation>) => {
Logger.info(TAG, `on signalInfoChange, data: ${JSON.stringify(data)}`)
detailData.signalInformation = data
}
cellularDataConnectionStateChangeCallback = (data: Value) => {
Logger.info(TAG, `on cellularDataConnectionStateChange, data: ${JSON.stringify(data)}`)
detailData.dataConnectState = data.state
detailData.ratType = data.network
}
cellularDataFlowChangeCallback = (data: data.DataFlowType) => {
Logger.info(TAG, `on cellularDataFlowChange, data: ${JSON.stringify(data)}`)
detailData.dataFlowType = data
}
simStateChangeCallback = (data: observer.SimStateData) => {
Logger.info(TAG, `on simStateChange, data: ${JSON.stringify(data)}`)
detailData.simStateData = data
}
onChangeCallback(index: number, isOn: boolean) {
switch (index) {
case 0:
isOn ? observer.on('networkStateChange', this.networkStateChangeCallback) :
observer.off('networkStateChange', this.networkStateChangeCallback)
break
case 1:
isOn ? observer.on('signalInfoChange', this.signalInfoChangeCallback) :
observer.off('signalInfoChange', this.signalInfoChangeCallback)
break
case 2:
isOn ? observer.on('cellularDataConnectionStateChange', this.cellularDataConnectionStateChangeCallback) :
observer.off('cellularDataConnectionStateChange', this.cellularDataConnectionStateChangeCallback)
break
case 3:
isOn ? observer.on('cellularDataFlowChange', this.cellularDataFlowChangeCallback) :
observer.off('cellularDataFlowChange', this.cellularDataFlowChangeCallback)
break
case 4:
isOn ? observer.on('simStateChange', this.simStateChangeCallback) :
observer.off('simStateChange', this.simStateChangeCallback)
break
default:
break
}
}
build() {
Column() {
Row() {
Text('observer')
.fontSize(30)
.fontColor(Color.White)
}
.width('100%')
.height('8%')
.padding({ left: 16 })
.backgroundColor('#0D9FFB')
List({ space: 20, initialIndex: 0 }) {
ForEach(this.subscribes, (item: Resource, index) => {
ListItem() {
Row() {
Text(item)
.fontSize(20)
Blank()
Toggle({ type: ToggleType.Switch, isOn: false })
.size({ width: 28, height: 16 })
.selectedColor('#0D9FFB')
.onChange((isOn: boolean) => {
this.onChangeCallback(index, isOn)
})
.id('switch' + (index + 1))
}
.width('100%')
.margin({bottom: 12 })
}
.width('100%')
})
}
.width('100%')
.height('70%')
.padding({ left: 10, right: 10 })
Button($r('app.string.details'))
.id('seeDetails')
.width('40%')
.onClick(() => {
Logger.info(TAG, `detailData is = ${JSON.stringify(detailData)}`)
router.pushUrl({
url: 'pages/Detail',
params: {
detailData: detailData
}
})
})
}
.width('100%')
.height('100%')
}
}
- 接口参考:@ohos.telephony.radio,@ohos.telephony.data,@ohos.telephony.observer
以上就是本篇文章所带来的鸿蒙开发中一小部分技术讲解;想要学习完整的鸿蒙全栈技术。可以在结尾找我可全部拿到!
下面是鸿蒙的完整学习路线,展示如下:
除此之外,根据这个学习鸿蒙全栈学习路线,也附带一整套完整的学习【文档+视频】,内容包含如下:
内容包含了:(ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、鸿蒙南向开发、鸿蒙项目实战)等技术知识点。帮助大家在学习鸿蒙路上快速成长!
鸿蒙【北向应用开发+南向系统层开发】文档
鸿蒙【基础+实战项目】视频
鸿蒙面经
为了避免大家在学习过程中产生更多的时间成本,对比我把以上内容全部放在了↓↓↓想要的可以自拿喔!谢谢大家观看!