API7+
说明:截至目前api12,在第三方应用中并不能够直接拨打电话,因此采用 跳转至拨号页面并将默认号码传输至拨号页面,由用户手动拨号 来实现拨号功能
功能:跳转至拨号页面并携带手机号
api: @ohos.telephony.call
步骤:
1.首先检查当前设备是否具备通话能力,通过 call.hasVoiceCapability 方法
如:let isSupport = call.hasVoiceCapability() 2.如果设备具备通话能力,通过 call.makeCall 方法跳转至拨号页面,参数1为携带过去的电话号码,参数二为回调函数,回调函数中返回跳转拨号界面的结果 如:
1. call.makeCall("13xxxx", (err: BusinessError) => {
1. if (!err) {
1. console.log("make call success.");
1. } else {
1. console.log("make call fail, err is:" + JSON.stringify(err));
1. }
1. });
demo:
// import需要的模块
import { call, observer } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 调用查询能力接口
let isSupport = call.hasVoiceCapability();
if (isSupport) {
// 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码
call.makeCall("13xxxx", (err: BusinessError) => {
if (!err) {
console.log("make call success.");
} else {
console.log("make call fail, err is:" + JSON.stringify(err));
}
});
// 订阅通话业务状态变化(可选)
class SlotId {slotId: number = 0}
class CallStateCallback {
state: call.CallState = call.CallState.CALL_STATE_UNKNOWN;
number: string = "";
}
let slotId: SlotId = {slotId: 0}
observer.on("callStateChange", slotId, (data: CallStateCallback) => {
console.log("call state change, data is:" + JSON.stringify(data));
});
}