OpenHarmony 电话服务
电话服务系统提供了一系列的 API 用于拨打电话、获取无线蜂窝网络和 SIM 卡相关信息。
应用可以通过调用 API 来获取当前注册网络名称、网络服务状态、信号强度以及 SIM 卡的相关信息,具体可参考获取当前蜂窝网络信号信息开发指导。
直接拨打电话需要系统权限 ohos.permission.PLACE_CALL,建议应用使用 makeCall(),跳转到拨号界面,并显示拨号的号码,具体可查看下面的演示。
OpenHarmony 跳转拨号界面
当应用需要跳转到拨号界面,并显示拨号的号码时,大家就可以来看这篇文章,当开发者调用 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/8/15 08:20 * @Description : 文件描述 */// import需要的模块import call from '@ohos.telephony.call';import observer from '@ohos.telephony.observer';@Entry@Componentstruct CAllTest{ build(){ Column(){ Button("打电话").width(200).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)}}
复制代码
感谢大家的支持。
评论