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

news2024/10/6 4:10:15

概述

本示例展示了电话服务中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 = undefined
  private sim2Text: Resource = undefined
  private 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: Resource
    try {
      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')
          break
        case sim.SimState.SIM_STATE_NOT_PRESENT:
          flagText = $r('app.string.not_present')
          break
        case sim.SimState.SIM_STATE_LOCKED:
          flagText = $r('app.string.locked')
          break
        case sim.SimState.SIM_STATE_NOT_READY:
          flagText = $r('app.string.not_ready')
          break
        case sim.SimState.SIM_STATE_READY:
          flagText = $r('app.string.ready')
          break
        case 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 = true
    Logger.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 = undefined
  private slotId: number
  controller: CustomDialogController

  async 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: string
    try {
      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.coloradmin.cn/o/1490719.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android Studio Iguana | 2023.2.1版本

Android Gradle 插件和 Android Studio 兼容性 Android Studio 构建系统基于 Gradle&#xff0c;并且 Android Gradle 插件 (AGP) 添加了一些特定于构建 Android 应用程序的功能。下表列出了每个版本的 Android Studio 所需的 AGP 版本。 如果特定版本的 Android Studio 不支持…

pandas数据分析42——读取和写入stata和spss的数据格式

python就是胶水语言&#xff0c;啥文件基本都能读取&#xff0c;而且pandas作为数据分析最好用的包&#xff0c;其功能自然也很多&#xff0c;可以读取各种数据文件。 本次就来演示一下怎么读取stata文件&#xff0c;和spss文件&#xff0c;他们不仅储存了数据和变量&#xff…

如何解决iQOO手机运行uniapp真机调试时无法识别的问题

打开开发者选项&#xff0c;打开USB设置&#xff0c;把默认USB选项改成MIDI模式&#xff0c;就可以检测到手机了

Golang高级微调技术

本文分享了一些小技巧&#xff0c;可以帮助我们写出更简化、高效的Golang代码&#xff0c;从而获得更好的开发体验。原文: Fine-Tuning Golang: Advanced Techniques for Code Optimization 本文是Golang代码优化技术的综合指南&#xff0c;帮助我们释放 Golang 应用程序的全部…

微信小程序(五十)请求拦截器实现携token获取用户信息

注释很详细&#xff0c;直接上代码 上一篇 新增内容&#xff1a; 1.个人信息框基本样式 2.请求拦截器携token获取个人信息进行渲染 源码&#xff1a; utils/http.js import http from "wechat-http"//设置全局默认请求地址 http.baseURL "https://live-api.it…

Mybatis_plus-基础

一、简介 1.概述 文档地址&#xff1a;https://baomidou.com/ 概述&#xff1a;MyBatis-Plus (opens new window)&#xff08;简称 MP&#xff09;是一个 MyBatis (opens new window) 的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、提…

前后端分离项目Docker部署指南(上)

目录 前言 一.搭建局域网 1.搭建net-ry局域网&#xff0c;用于部署若依项目 2.注意点 二.安装redis 创建目录 将容器进行挂载 ​编辑 测试是否安装成功 ​编辑 三. 安装MySQL 创建文件夹 上传配置文件并且修改 .启动MySQL容器服务 充许远程连接 四.部署后端 使用…

RISCV 中断控制器 PLIC APLIC (非MSI部分)

以下包含了 PLIC & APLIC 着重解释 APLIC 部分 参考 github 中 riscv-aia 规范1.0 &#xff0c;第四章 APLIC 注&#xff1a;关于MSI的部分简略&#xff08;等后续搞清楚&#xff09; 本人处于学习阶段&#xff0c;不清晰的地方&#xff0c;请见谅 一、关于 PLIC 配置 1、…

【Python】-----基础知识

注释 定义&#xff1a;让计算机跳过这个代码执行用三个单引号/双引号都表示注释信息&#xff0c;在Python中单引号与双引号没有区别&#xff0c;但必须是成对出现 输出与输入 程序是有开始&#xff0c;有结束的&#xff0c;程序运行规则&#xff1a;从上而下&#xff0c;由内…

(1)预处理

我们需要的文件结构如上 main.cpp add.h add.cpp add.h 这里使用riscv的工具链编译为.i文件&#xff0c;需要使用-E&#xff0c;就是只进行预处理&#xff0c;我们可以得到两个.i文件即main.i和add.i main.i 这里看到main.i里头文件全部替换&#xff0c;然后多了三万多行 所以…

力扣经典题目解析--合并两个有序链表

原题地址: . - 力扣&#xff08;LeetCode&#xff09; 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1&#xff1a; 输入&#xff1a;l1 [1,2,4], l2 [1,3,4] 输出&#xff1a;[1,1,2,3,4,4] 迭代 链表节点结构…

Day12-【Java SE进阶】JDK8新特性:Lambda表达式、方法引用、常见算法、正则表达式、异常

一、JDK8新特性 1.Lambda表达式 Lambda表达式是JDK 8开始新增的一种语法形式;作用:用于简化名内部类的代码写法。 注意:Lambda表达式并不是说能简化全部匿名内部类的写法&#xff0c;只能简化函数式接口的匿名内部类。 有且仅有一个抽象方法的接口。注意:将来我们见到的大部…

【音视频开发好书推荐】《RTC程序设计:实时音视频权威指南》

1、WebRTC概述 WebRTC&#xff08;Web Real-Time Communication&#xff09;是一个由Google发起的实时音视频通讯C开源库&#xff0c;其提供了音视频采集、编码、网络传输&#xff0c;解码显示等一整套音视频解决方案&#xff0c;我们可以通过该开源库快速地构建出一个音视频通…

【前端寻宝之路】总结学习使用CSS的引入方式

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法|MySQL| ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-BNJBIEvpN0GHNeJ1 {font-family:"trebuchet ms",verdana,arial,sans-serif;f…

C语言第三十五弹---文件操作(上)

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】 文件操作 1、为什么使用文件&#xff1f; 2、什么是文件&#xff1f; 2.1、程序文件 2.2、数据文件 2.3、文件名 3、二进制文件和文本文件 4、文件的打开和…

集成算法(随机森林,AdaBoost,Xgboost,Stacking模型)

目录 一、前言 二、Bagging模型 三、Boosting模型 四、Stacking模型 五、总结 一、前言 集成算法&#xff08;Enseamable learning&#xff09; 集成算法一般考虑树模型&#xff0c;KNN就不太适合 目的&#xff1a;让机器学习效果更好&#xff0c;单个不好&#xff0c;一起…

性能优化篇(七) UI优化注意事项以及使用Sprite Atlas打包精灵图集

UI优化注意事项 1.尽量避免使用IMGUI(OnGUI)来做游戏时的UI&#xff0c;因为IMGUI的开销比较大。 2.如果一个UGUI的控件不需要进行射线检测&#xff0c;则可以取消勾选Raycast Target 3.尽量避免使用完全透明的图片和UI控件。因为即使完全透明&#xff0c;我们看不见它&#xf…

BUUCTF------[HCTF 2018]WarmUp

开局一个表情&#xff0c;源代码发现source.php <?phphighlight_file(__FILE__);class emmm{public static function checkFile(&$page){$whitelist ["source">"source.php","hint">"hint.php"];if (! isset($page) |…

关于图形学中生成三角形库Triangle.Net的下载及简单使用

背景 Triangle.NET 是生成 2D&#xff08;约束&#xff09;Delaunay 三角剖分和点集或平面直线图的高质量网格。此库是基于 Jonathan Richard Shewchuk 的 Triangle 项目&#xff0c;主要用于图形学像Opengl(或Unity3D)中生成三角形使用。 下载 Triangle.Net是基于C#语言的库…

倾斜三维模型OSGB路径漫游——DasViewer

背景 有时候我们需要查看倾斜三维模型效果的效果&#xff0c;虽然也有很多软件可以用&#xff0c;比如CC、超图等等&#xff0c;但是这些软件都比较大&#xff0c;安装也比较麻烦&#xff0c;DasViewer这个软件就比较轻量&#xff0c;安装也简单&#xff0c;功能强大&#xff…