鸿蒙实战开发:【SIM卡管理】

概述

本示例展示了电话服务中SIM卡相关功能,包含SIM卡的服务提供商、ISO国家码、归属PLMN号信息,以及默认语音卡功能。

样例展示

基础信息

介绍

本示例使用sim相关接口,展示了电话服务中SIM卡相关功能,包含SIM卡的服务提供商、ISO国家码、归属PLMN号信息,以及默认语音卡功能。

效果预览

使用说明:

1.若SIM卡槽1插入SIM卡则SIM卡1区域显示为蓝色,否则默认为白色。

2.点击SIM卡1区域,弹窗显示SIM卡1的相关信息,再次点击面板消失。

3.默认拨号的SIM卡其按钮背景色为蓝色,目前只展示默认拨号的SIM卡,更改默认拨号卡功能暂不支持。

4.呼叫转移界面功能暂不支持,故点击按钮无实际操作。

具体实现

  • 该示例主要通过hasSimCard方法获取指定卡槽SIM卡是否插卡,getSimState方法获取指定卡槽的SIM卡状态,SimState方法判断SIM卡状态,isSimActive方法获取指定卡槽SIM卡是否激活,getSimSpn方法获取指定卡槽SIM卡的服务提供商名称,getISOCountryCodeForSim方法获取指定卡槽SIM卡的ISO国家码,getSimOperatorNumeric方法获取指定卡槽SIM卡的归属PLMN号,getDefaultVoiceSlotId方法获取默认语音业务的卡槽ID等开发电话服务的相关功能。

源码链接:

InfoView.ets

/** 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 sim from '@ohos.telephony.sim'
import Logger from '../model/Logger'
import { SimView } from '../common/SimView'
import { CallView } from '../common/CallView'
import { Show } from '../common/Show'const TAG = '[InfoView]'
const card1 = 0
const card2 = 1@Component
export struct InfoView {@State data: string = ''@State slotId: number = 0@State flag: boolean = false@State sim1Color: string = '#FFFFFF'@State sim2Color: string = '#FFFFFF'private sim1Text: Resource = undefinedprivate sim2Text: Resource = undefinedprivate firstName: string = 'card1'private secondNAme: string = 'card2'async getColor(slotId: number) {let color: string = ''try {let result = await sim.hasSimCard(slotId)Logger.info(TAG, `color result is ${result}`)color = result ? '#0D9FFB' : '#FFFFFF'} catch (err) {color = '#FFFFFF'Logger.info(TAG, `err is ${JSON.stringify(err)}`)}Logger.info(TAG, `color is ${JSON.stringify(color)}`)return color}async getTextData(slotId: number) {let flagText: Resourcetry {let result = await sim.getSimState(slotId)Logger.info(TAG, `getSimState is ${result}`)switch (result) {case sim.SimState.SIM_STATE_UNKNOWN:flagText = $r('app.string.unknown')breakcase sim.SimState.SIM_STATE_NOT_PRESENT:flagText = $r('app.string.not_present')breakcase sim.SimState.SIM_STATE_LOCKED:flagText = $r('app.string.locked')breakcase sim.SimState.SIM_STATE_NOT_READY:flagText = $r('app.string.not_ready')breakcase sim.SimState.SIM_STATE_READY:flagText = $r('app.string.ready')breakcase sim.SimState.SIM_STATE_LOADED:flagText = $r('app.string.loaded')break}Logger.info(TAG, `flagText is ${JSON.stringify(flagText)}`)} catch (err) {flagText = $r('app.string.err')Logger.info(TAG, `err is ${JSON.stringify(err)} flagText is ${JSON.stringify(flagText)}`)}return flagText}async aboutToAppear() {this.sim1Text = await this.getTextData(card1)this.sim2Text = await this.getTextData(card2)this.sim1Color = await this.getColor(card1)this.sim2Color = await this.getColor(card2)this.flag = trueLogger.info(TAG, `sim1Text is ${JSON.stringify(this.sim1Text)} sim2Text is ${JSON.stringify(this.sim2Text)}`)}build() {Scroll() {Column() {if (this.flag) {Show({slotId: card1,simText: this.sim1Text,simColor: this.sim1Color,simTitle: $r('app.string.sim1_id'),simCard: $r('app.string.sim1_card'),idName: this.firstName})Show({slotId: card2,simText: this.sim2Text,simColor: this.sim2Color,simTitle: $r('app.string.sim2_id'),simCard: $r('app.string.sim2_card'),idName: this.secondNAme})}SimView()CallView()}}.layoutWeight(1)}
}

ShowView.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 sim from '@ohos.telephony.sim'
import Logger from '../model/Logger'const TAG = '[ShowView]'@CustomDialog
export struct ShowView {@State simState: Resource = undefined@State results: Array<unknown> = []@State simTitle: Resource = undefinedprivate slotId: numbercontroller: CustomDialogControllerasync simResult() {Logger.info(TAG, `getResult this.slotId ${this.slotId}`)this.simTitle = this.slotId === 0 ? $r('app.string.sim1_state') : $r('app.string.sim2_state')let result = await sim.isSimActive(this.slotId)this.simState = result ? $r('app.string.sim_activation') : $r('app.string.sim_inactivated')}async getSimData() {let data: Array<string | Resource> = new Array(3).fill('')Logger.info(TAG, `data = ${JSON.stringify(data)}`)try {data[0] = await sim.getSimSpn(this.slotId)Logger.info(TAG, `data = ${JSON.stringify(data[0])}`)} catch (err) {data[0] = $r('app.string.err')Logger.info(TAG, `data = ${JSON.stringify(data[0])} err = ${JSON.stringify(err)}`)}try {data[1] = await sim.getISOCountryCodeForSim(this.slotId)Logger.info(TAG, `data = ${JSON.stringify(data[1])}`)} catch (err) {data[1] = $r('app.string.err')Logger.info(TAG, `data = ${JSON.stringify(data[1])} err = ${JSON.stringify(err)}`)}try {data[2] = await sim.getSimOperatorNumeric(this.slotId)Logger.info(TAG, `data = ${JSON.stringify(data[2])}`)} catch (err) {data[2] = $r('app.string.err')Logger.info(TAG, `data = ${JSON.stringify(data[2])} err = ${JSON.stringify(err)}`)}Logger.info(TAG, `data is ${JSON.stringify(data)}`)return data}async aboutToAppear() {await this.simResult()let result = await this.getSimData()Logger.info(TAG, `result = ${JSON.stringify(result)}`)this.results = [{ title: $r('app.string.spn'), value: result[0] }, { title: $r('app.string.iso'), value: result[1] },{ title: $r('app.string.plmn'), value: result[2] }]Logger.info(TAG, `results = ${JSON.stringify(this.results)}`)}build() {Column() {Text(this.simTitle).fontSize(18).margin({ left: 5, right: 5, top: 5, bottom: 10 })Text($r('app.string.active')).margin(5).fontSize(18).fontColor(Color.Gray)Text(this.simState).margin(5).fontSize(18)ForEach(this.results, item => {Text(item.title).margin(5).fontSize(18).fontColor(Color.Gray)Text(item.value).margin(5).fontSize(18)}, item => JSON.stringify(item))}.margin(10).padding(5).width('100%').borderRadius(10).alignItems(HorizontalAlign.Start).onClick(() => {this.controller.close()Logger.info(TAG, ` CustomDialog close`)})}
}

SimView.ets

/** 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 sim from '@ohos.telephony.sim'
import Logger from '../model/Logger'const TAG = '[SimView]'
const card1 = 0
const card2 = 1@Component
export struct SimView {@State data: string = ''@State sim1Color: string = '#FFFFFF'@State sim2Color: string = '#FFFFFF'async getDefaultVoice(num: number) {let color: stringtry {let result = await sim.getDefaultVoiceSlotId()Logger.info(TAG, `color result is ${result}`)color = result === num ? '#0D9FFB' : '#FFFFFF'Logger.info(TAG, `color is ${JSON.stringify(color)}`)} catch (err) {color = '#FFFFFF'Logger.info(TAG, `err is ${JSON.stringify(err)} color fail is ${JSON.stringify(color)}`)}return color}async aboutToAppear() {[this.sim1Color, this.sim2Color] = await Promise.all([this.getDefaultVoice(card1), this.getDefaultVoice(card2)])Logger.info(TAG, `sim1Color is ${this.sim1Color} sim2Color is ${this.sim2Color}`)}build() {Column() {Row() {Text($r('app.string.voice')).fontSize(20).fontColor(Color.Gray)Blank()Row() {Button() {Text($r('app.string.sim1_id')).fontSize(18).fontColor(Color.Black).textAlign(TextAlign.Center)}.width('50%').height('85%').padding(5).borderRadius(10).backgroundColor(this.sim1Color)Button() {Text($r('app.string.sim2_id')).fontSize(18).fontColor(Color.Black).textAlign(TextAlign.Center)}.width('50%').height('85%').padding(5).borderRadius(10).backgroundColor(this.sim2Color)}.width('40%').height('95%').borderRadius(50).backgroundColor('#F1F1F1')}.margin(8).padding(10).width('95%').height('8%').borderRadius(10).backgroundColor(Color.White)}}
}

鸿蒙OpenHarmony知识待更新👈点

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/511415.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣每日一题 用栈实现队列

Problem: 232. 用栈实现队列 文章目录 思路复杂度&#x1f496; 朴素版&#x1f496; 优化版 思路 &#x1f468;‍&#x1f3eb; 路飞题解 复杂度 时间复杂度: 添加时间复杂度, 示例&#xff1a; O ( n ) O(n) O(n) 空间复杂度: 添加空间复杂度, 示例&#xff1a; O ( …

Qt+FFmpeg+opengl从零制作视频播放器-1.项目介绍

1.简介 学习音视频开发&#xff0c;首先从做一款播放器开始是比较合理的&#xff0c;每一章节&#xff0c;我都会将源码贴在最后&#xff0c;此专栏你将学习到以下内容&#xff1a; 1&#xff09;音视频的解封装、解码&#xff1b; 2&#xff09;Qtopengl如何渲染视频&#…

sql单表运用11.3

一、进入数据库操作界面 1、mysql -u root -p 敲回车 &#xff0c;输入密码 &#xff0c;进入数据库操作界面 2、show databases 查看所有的数据&#xff08;如果没有数据库&#xff1a;创建数据库 create database 库名称&#xff09; 3、use 数据库名 使…

IDEA中Maven无法下载jar包问题解决

在项目中经常会遇到jar包无法下载的问题&#xff0c;可以根据以下几种方法进行排查。 1. 排查网络连接 网络连接失败&#xff0c;会导致远程访问Maven仓库失败&#xff0c;所以应确保网络连接正常。 2. 排查Maven的配置 Maven配置文件&#xff08;settings.xml&#xff09;…

交友盲盒系统PHP开源的盲盒源码

源码介绍&#xff1a; 交友盲盒系统是一款基于PHP开发的开源免费盲盒系统&#xff0c;旨在为用户提供一个充满乐趣和惊喜的社交体验。该系统具有丰富的功能和灵活的扩展性&#xff0c;可以轻松地满足各种线上交友、抽奖活动等场景的需求。 安装说明&#xff1a; PHP版本&…

基于springboot+vue线上教育平台管理系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现线上教育平台管理系统演示 摘要 本文讲述了使用SSM框架及My Sql数据库技术开发的线上教育网站的设计与实现。本系统是一个可以让学生进行在线学习的网站&#xff0c;众所周知&#xff0c;计算机专业的难度是比较高的&#xff0c;如果只通过在课堂上的学习&a…

【性能测试】Jmeter性能压测-阶梯式/波浪式场景总结(详细)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、阶梯式场景&am…

docker 安装 portainer

小编给友友们总结了一下 Portainer 的好处以下 Portainer是Docker的图形化管理工具&#xff0c;提供状态显示面板、应用模板快速部署、容器镜像网络数据卷的基本操作&#xff08;包括上传下载镜像&#xff0c;创建容器等操作&#xff09;、事件日志显示、容器控制台操作、Swar…

LiveGBS流媒体平台GB/T28181功能-集中录像存储前端设备录像回看解决方案设备录像|云端录像|实时录像说明

LiveGBS集中录像存储前端设备录像回看解决方案设备录像|云端录像|实时录像说明 1、平台概述2、视频录像2.1、设备录像2.1.1、存储位置2.1.1.1、下级硬件设备2.1.1.2、下级国标平台 2.1.2、页面操作2.1.2.1、国标设备2.1.2.1.1、查看通道2.1.2.1.1.1、设备录像 2.1.2.1.2、配置中…

VS2022连接数据库以及常用的连接函数

下面是如何配置以及设置VS2022连接数据库 第一步:打开mysql的安装目录&#xff0c;默认安装目录如下&#xff1a;C:\Program Files\MySQL\MySQL Server 8.0&#xff0c;确认 lib 目录和include 目录是否存在。 第二步&#xff1a;打开VS2019&#xff0c;新建一个空工程,控制台…

huggingface学习|controlnet实战:云服务器使用StableDiffusionControlNetPipeline生成图像

ControlNet核心基础知识 文章目录 一、环境配置和安装需要使用的库二、准备数据及相关模型三、参照样例编写代码&#xff08;一&#xff09;导入相关库&#xff08;二&#xff09;准备数据&#xff08;以知名画作《戴珍珠耳环的少女》为例&#xff09;&#xff08;三&#xff0…

web小游戏,蜘蛛纸牌

H5小游戏源码、JS开发网页小游戏开源源码大合集。无需运行环境,解压后浏览器直接打开。有需要的订阅后,私信本人,发源码,含60+小游戏源码。如五子棋、象棋、植物大战僵尸、贪吃蛇、飞机大战、坦克大战、开心消消乐、扑鱼达人、扫雷、打地鼠、斗地主等等。 <!DOCTYPE h…