> 文档中心 > #DAYU200# OpenHarmony跳转拨号界面

#DAYU200# OpenHarmony跳转拨号界面


日常交流中,给朋友打电话是一个常见的交流方式,那么如何在OpenHarmony中进行电话服务相关的开发呢,今天我们可以一起来了解一下。

电话服务系统提供了一系列的API用于拨打电话、获取无线蜂窝网络和SIM卡相关信息。

应用可以通过调用API来获取当前注册网络名称、网络服务状态、信号强度以及SIM卡的相关信息,具体可参考获取当前蜂窝网络信号信息开发指导。

直接拨打电话需要系统权限ohos.permission.PLACE_CALL,建议应用使用makeCall(),跳转到拨号界面,并显示拨号的号码,当开发者调用makeCall接口时,设备会自动跳转到拨号界面。和正常拨打电话一样,用户可以选择卡1或卡2拨出。

接口说明

call模块为开发者提供呼叫管理功能。observer模块为开发者提供通话业务状态订阅和取消订阅功能。

  • call.hasVoiceCapability():能力获取,表示是否具有语音功能。

  • call.makeCall()跳转拨号界面,跳转到拨号界面,并显示拨号的号码。

  • observer.on(‘callStateChange’):订阅通话业务状态变化,ohos.permission.READ_CALL_LOG (获取通话号码需要该权限)

  • observer.off(‘callStateChange’):取消订阅通话业务状态变化.

开发步骤

1.import需要的模块。

// import需要的模块import call from '@ohos.telephony.call';import observer from '@ohos.telephony.observer';

2.调用hasVoiceCapability()接口获取当前设备呼叫能力,如果支持继续下一步;如果不支持则无法发起呼叫。

// 调用查询能力接口let isSupport = call.hasVoiceCapability();if (!isSupport) {    console.log("not support voice capability, return.");    return;}

3.跳转到拨号界面,并显示拨号的号码。

// 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码call.makeCall("13xxxx", (err)=> {    if (!err) {        console.log("make call success.");    } else {        console.log("make call fail, err is:" + JSON.stringify(err));    }});

4.(可选)订阅通话业务状态变化。

// 订阅通话业务状态变化(可选)observer.on("callStateChange", (data) => {    console.log("call state change, data is:" + JSON.stringify(data));});

5.取消订阅通话业务状态变。

// 取消订阅通话业务状态变observer.off("callStateChange", (data) => {    console.log("call state change, data is:" + JSON.stringify(data));});

完毕

最后附上完整代码:

/* * Copyright (c) 2022 JianGuo 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. *//** * @ProjectName : nutsStudy * @FileName :  call * @Author : 坚果 * @Time : 2022/9/15 08:20 * @Description : 文件描述 */// import需要的模块import call from '@ohos.telephony.call';import observer from '@ohos.telephony.observer';@Entry@Componentstruct CAllTest{  build(){​    Column(){​      Button("打电话给坚果").width(300).height(80) .fontSize(30).fontColor(Color.Orange).onClick(()=>{        // 调用查询能力接口        let isSupport = call.hasVoiceCapability();        if (!isSupport) {          console.info(" support voice capability, return");          return;        }        // 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码        call.makeCall("17752170152", (err)=> {          if (!err) {            console.info(" make call success.");​          } else {            console.info("make call fail, err is:" + JSON.stringify(err));          }        });​​      })​    }.width("100%").height("100%").justifyContent(FlexAlign.Center)​​  }}

好的,今天的一个小功能就带大家了解到这儿,大家也可以将这个功能扩展到自己的应用里。

身份验证指纹锁商城