HarmonyOS电话服务相关

213 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第33天,点击查看活动详情

@ohos.telephony.data (蜂窝数据)

import data from '@ohos.telephony.data';

因为蜂窝数据是敏感数据,所以需要权限。

这个时候就需要在config.JSON或者module.json5中去配置

FA

"reqPermissions": [
  {
  "name": "ohos.permission.GET_NETWORK_INFO"
  }
]

Stage

"requestPermissions": [
  {
   "name": "ohos.permission.GET_NETWORK_INFO"
  }
]

data.getDefaultCellularDataSlotId

getDefaultCellularDataSlotId(): Promise

获取默认移动数据的SIM卡,使用Promise方式作为异步方法。

需要权限:ohos.permission.GET_NETWORK_INFO

系统能力:SystemCapability.Telephony.CellularData

返回值:

类型说明
Promise以Promise形式返回获取默认移动数据的SIM卡。0:卡槽1。1:卡槽2。

示例:

import data from '@ohos.telephony.data';
​
@Entry
@Component
struct Index {
  @State message: string = 'Hello '
​
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(()=>{
          let promise = data.getDefaultCellularDataSlotId();
          promise.then((data) => {
            console.log(`test success, promise: data->${JSON.stringify(data)}`);
            
          }).catch((err) => {
            console.error(`test fail, promise: err->${JSON.stringify(err)}`);
          });
​
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

@ohos.telephony.call (拨打电话)

该模块提供呼叫管理功能,包括拨打电话、跳转到拨号界面、获取通话状态、格式化电话号码等。

使用的时候需要导入模块

import call from '@ohos.telephony.call';

因为拨打电话是敏感数据,所以需要权限。

这个时候就需要在config.JSON或者module.json5中去配置

FA

"reqPermissions": [
  {
  "name": "ohos.permission.PLACE_CALL"
  }
]

Stage

"requestPermissions": [
  {
   "name": "ohos.permission.PLACE_CALL"
  }
]

call.makeCall7+

makeCall(phoneNumber: string): Promise

跳转到拨号界面,并显示待拨出的号码。使用Promise方式作为异步方法。

import call from '@ohos.telephony.call';
import data from '@ohos.telephony.data';
​
@Entry
@Component
struct Index {
  @State message: string = 'Hello '
​
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(()=>{
          let promise = call.makeCall("17752170152");
          promise.then(() => {
            console.log(`makeCall success`);
          }).catch(err => {
            console.error(`makeCall fail, promise: err->${JSON.stringify(err)}`);
          });
​
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

@ohos.telephony.observer (observer)

导入模块

import observer from '@ohos.telephony.observer'

因为蜂窝数据是敏感数据,所以需要权限。

这个时候就需要在config.JSON或者module.json5中去配置

FA

"reqPermissions": [
  {
  "name": "ohos.permission.GET_NETWORK_INFO"
  }
]

Stage

"requestPermissions": [
  {
   "name": "ohos.permission.GET_NETWORK_INFO"
  }
]

observer.on('signalInfoChange')

on(type: 'signalInfoChange', options: { slotId: number }, callback: Callback<Array>): void;

订阅指定卡槽位的信号状态变化事件,使用callback方式作为异步方法。

系统能力:SystemCapability.Telephony.StateRegistry

参数:

参数名类型必填说明
typestring信号状态变化事件
slotIdnumber卡槽ID。- 0:卡槽1- 1:卡槽2
callbackCallback<Array回调函数。参考radio的SignalInformation
import observer from '@ohos.telephony.observer';
​
​
@Entry
@Component
struct Index {
  @State message: string = 'Hello '
​
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(()=>{
          observer.on('signalInfoChange', data =>{
            this.message=JSON.stringify(data)
            console.log("on signalInfoChange, data:" + JSON.stringify(data));
          });
​
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

运行代码后,控制台打印如下所示

data:{"array":[{"networkType":5,"signalLevel":5,"signalStrength":-90}]

SignalInformation

网络信号强度信息对象。

系统能力:以下各项对应的系统能力均为SystemCapability.Telephony.CoreService。

参数名类型说明
signalTypeNetworkType网络信号强度类型。
signalLevelnumber网络信号强度等级

NetworkType

网络类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Telephony.CoreService。

名称说明
NETWORK_TYPE_UNKNOWN0未知网络类型。
NETWORK_TYPE_GSM1网络类型为GSM(Global System For Mobile Communication)。
NETWORK_TYPE_CDMA2网络类型为CDMA(Code Division Multiple Access)。
NETWORK_TYPE_WCDMA3网络类型为WCDMA(Wideband Code Division Multiple Access)。
NETWORK_TYPE_TDSCDMA4网络类型为TDSCDMA(TimeDivision-Synchronous Code Division Multiple Access)。
NETWORK_TYPE_LTE5网络类型为LTE(Long Term Evolution)。
NETWORK_TYPE_NR6网络类型为5G NR(New Radio)。