鸿蒙开发电话服务:【@ohos.telephony.call (拨打电话)】

拨打电话

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

如需订阅通话状态请使用`observer.on('callStateChange')`

说明:

本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

复制代码
import call from '@ohos.telephony.call';

call.dial

dial(phoneNumber: string, callback: AsyncCallback): void

拨打电话。使用callback异步回调。

需要权限:ohos.permission.PLACE_CALL,该权限为系统权限

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
callback AsyncCallback 是 回调函数,返回true为成功,false为失败。

示例:

复制代码
call.dial("138xxxxxxxx", (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.dial

dial(phoneNumber: string, options: DialOptions, callback: AsyncCallback): void

拨打电话,可设置通话参数。使用callback异步回调。

需要权限:ohos.permission.PLACE_CALL,该权限为系统权限

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options DialOptions 是 通话参数,选择为语音通话还是视频通话。
callback AsyncCallback 是 回调函数,返回true为成功,false为失败。

示例:

复制代码
call.dial("138xxxxxxxx", {
    extras: false
}, (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.dial

dial(phoneNumber: string, options?: DialOptions): Promise

拨打电话,可设置通话参数。使用Promise异步回调。

需要权限:ohos.permission.PLACE_CALL,该权限为系统权限

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options DialOptions 是 通话参数,选择为语音通话还是视频通话。

返回值:

类型 说明
Promise 以Promise形式返回拨打电话的结果,返回true为成功,false为失败。

示例:

复制代码
let promise = call.dial("138xxxxxxxx", {
    extras: false
});
promise.then(data => {
    console.log(`dial success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`dial fail, promise: err->${JSON.stringify(err)}`);
});

call.makeCall7+

makeCall(phoneNumber: string, callback: AsyncCallback): void

跳转到拨号界面,并显示待拨出的号码。使用callback异步回调。

系统能力:SystemCapability.Applications.Contacts

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
callback AsyncCallback 是 以callback形式异步返回跳转拨号界面的结果。

示例:

复制代码
call.makeCall("138xxxxxxxx", err => { 
    console.log(`makeCall callback: err->${JSON.stringify(err)}`); 
});

call.makeCall7+

makeCall(phoneNumber: string): Promise

跳转到拨号界面,并显示待拨出的号码。使用Promise异步回调。

系统能力:SystemCapability.Applications.Contacts

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。

返回值:

类型 说明
Promise 以Promise形式异步返回拨号的结果。

示例:

复制代码
let promise = call.makeCall("138xxxxxxxx"); 
promise.then(() => { 
    console.log(`makeCall success`); 
}).catch(err => { 
    console.error(`makeCall fail, promise: err->${JSON.stringify(err)}`); 
});

call.hasCall

hasCall(callback: AsyncCallback): void

判断是否存在通话。使用callback异步回调。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
callback AsyncCallback 是 回调函数。返回true表示当前存在通话,false表示当前不存在通话。

示例:

复制代码
call.hasCall((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.hasCall

hasCall(): Promise

判断是否存在通话。使用Promise异步回调。

系统能力:SystemCapability.Telephony.CallManager

返回值:

类型 说明
Promise 以Promise形式异步返回判断是否存在通话。

示例:

复制代码
let promise = call.hasCall();
promise.then(data => {
    console.log(`hasCall success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`hasCall fail, promise: err->${JSON.stringify(err)}`);
});

call.getCallState

getCallState(callback: AsyncCallback): void

获取当前通话状态。使用callback异步回调。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
callback AsyncCallback<CallState> 是 回调函数,异步返回获取到的通话状态。

示例:

复制代码
call.getCallState((err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.getCallState

getCallState(): Promise

获取当前通话状态。使用Promise异步回调。

系统能力:SystemCapability.Telephony.CallManager

返回值:

类型 说明
Promise<CallState> 以Promise形式异步返回获取到的通话状态。

示例:

复制代码
let promise = call.getCallState();
promise.then(data => {
    console.log(`getCallState success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`getCallState fail, promise: err->${JSON.stringify(err)}`);
});

call.hasVoiceCapability7+

hasVoiceCapability(): boolean

检查当前设备是否具备语音通话能力。

系统能力:SystemCapability.Telephony.CallManager

返回值:

类型 说明
boolean 返回true表示设备具备语音通话能力,返回false表示设备不具备语音通话能力。
复制代码
let result = call.hasVoiceCapability(); 
console.log(`hasVoiceCapability: ${JSON.stringify(result)}`);

call.isEmergencyPhoneNumber7+

isEmergencyPhoneNumber(phoneNumber: string, callback: AsyncCallback): void

判断是否是紧急电话号码。使用callback异步回调。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
callback AsyncCallback 是 回调函数。返回true表示是紧急电话号码,返回false表示不是紧急电话号码。

示例:

复制代码
call.isEmergencyPhoneNumber("138xxxxxxxx", (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.isEmergencyPhoneNumber7+

isEmergencyPhoneNumber(phoneNumber: string, options: EmergencyNumberOptions, callback: AsyncCallback): void

根据电话号码参数,判断是否是紧急电话号码。使用callback异步回调。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options EmergencyNumberOptions 是 电话号码参数。
callback AsyncCallback 是 回调函数。返回true表示是紧急电话号码,返回false表示不是紧急电话号码。

示例:

复制代码
call.isEmergencyPhoneNumber("112", {slotId: 1}, (err, value) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.isEmergencyPhoneNumber7+

isEmergencyPhoneNumber(phoneNumber: string, options?: EmergencyNumberOptions): Promise

根据电话号码参数,判断是否是紧急电话号码。使用Promise异步回调。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options EmergencyNumberOptions 是 电话号码参数。

返回值:

类型 说明
Promise 以Promise形式异步返回判断是否是紧急电话号码的结果。

示例:

复制代码
let promise = call.isEmergencyPhoneNumber("138xxxxxxxx", {slotId: 1});
promise.then(data => {
    console.log(`isEmergencyPhoneNumber success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`isEmergencyPhoneNumber fail, promise: err->${JSON.stringify(err)}`);
});

call.formatPhoneNumber7+

formatPhoneNumber(phoneNumber: string, callback: AsyncCallback): void

格式化电话号码。使用callback异步回调。

电话号码格式化后为标准数字字串,例如:"138 xxxx xxxx"、"0755 xxxx xxxx"。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
callback AsyncCallback 是 回调函数,返回格式化电话号码的结果。

示例:

复制代码
call.formatPhoneNumber("138xxxxxxxx", (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.formatPhoneNumber7+

formatPhoneNumber(phoneNumber: string, options: NumberFormatOptions, callback: AsyncCallback): void

格式化电话号码,可设置格式化参数。使用callback异步回调。

电话号码格式化后为标准数字字串,例如:"138 xxxx xxxx"、"0755 xxxx xxxx"。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options NumberFormatOptions 是 格式化参数,如国家码。
callback AsyncCallback 是 回调函数,返回格式化电话号码的结果。

示例:

复制代码
call.formatPhoneNumber("138xxxxxxxx",{
    countryCode: "CN"
}, (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.formatPhoneNumber7+

formatPhoneNumber(phoneNumber: string, options?: NumberFormatOptions): Promise

格式化电话号码,可设置格式化参数。使用Promise异步回调。

电话号码格式化后为标准数字字串,例如:"138 xxxx xxxx"、"0755 xxxx xxxx"。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
options NumberFormatOptions 是 格式化参数,如国家码。

返回值:

类型 说明
Promise 以Promise形式异步返回格式化电话号码的结果。

示例:

复制代码
let promise = call.formatPhoneNumber("138xxxxxxxx", {
    countryCode: "CN"
});
promise.then(data => {
    console.log(`formatPhoneNumber success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`formatPhoneNumber fail, promise: err->${JSON.stringify(err)}`);
});

call.formatPhoneNumberToE1647+

formatPhoneNumberToE164(phoneNumber: string, countryCode: string, callback: AsyncCallback): void

将电话号码格式化为E.164表示形式。使用callback异步回调。

待格式化的电话号码需要与传入的国家码相匹配,如中国电话号码需要传入国家码CN,否则格式化后的电话号码为null。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
countryCode string 是 国家码,支持所有国家码,如:中国(CN)。
callback AsyncCallback 是 回调函数,返回将电话号码格式化为E.164表示形式的结果。

示例:

复制代码
call.formatPhoneNumberToE164("138xxxxxxxx",{
    countryCode: "CN"
}, (err, data) => {
    console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});

call.formatPhoneNumberToE1647+

formatPhoneNumberToE164(phoneNumber: string, countryCode: string): Promise

将电话号码格式化为E.164表示形式。使用Promise异步回调。

待格式化的电话号码需要与传入的国家码相匹配,如中国电话号码需要传入国家码CN,否则格式化后的电话号码为null。

支持所有国家码。

系统能力:SystemCapability.Telephony.CallManager

参数:

参数名 类型 必填 说明
phoneNumber string 是 电话号码。
countryCode string 是 国家码,支持所有国家码,如:中国(CN)。

返回值:

类型 说明
Promise 以Promise形式异步返回将电话号码格式化为E.164表示形式的结果。

示例:

复制代码
let promise = call.formatPhoneNumberToE164("138xxxxxxxx", {
    countryCode: "CN"
});
promise.then(data => {
    console.log(`formatPhoneNumberToE164 success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
    console.error(`formatPhoneNumberToE164 fail, promise: err->${JSON.stringify(err)}`);
});

DialOptions

拨打电话的可选参数。

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

参数名 类型 必填 说明
extras boolean 否 根据extras的值判断是否为视频通话,默认为语音通话。 - true:视频通话。 - false:语音通话。

CallState

通话状态码。

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

名称 值 说明
CALL_STATE_UNKNOWN -1 无效状态,当获取呼叫状态失败时返回。
CALL_STATE_IDLE 0 表示没有正在进行的呼叫。
CALL_STATE_RINGING 1 表示来电正在振铃或等待。
CALL_STATE_OFFHOOK 2 表示至少有一个呼叫处于拨号、通话中或呼叫保持状态,并且没有新的来电振铃或等待。

EmergencyNumberOptions7+

判断是否是紧急电话号码的可选参数。

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

参数名 类型 必填 说明
slotId number 否 卡槽ID: - 卡槽1为0。 - 卡槽2为1。

NumberFormatOptions7+

格式化号码的可选参数。

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

参数名 类型 必填 说明
countryCode string 否 国家码,支持所有国家的国家码,如:中国(CN)。默认为:CN。
相关推荐
Sirius_Ji1 小时前
# 一份鸿蒙原生支持硬解的 ffmpeg 开发基座
harmonyos
贾伟康2 小时前
HarmonyOS API 26 Navigation路由栈上限:stackSizeLimit、过载保护与容量观测
harmonyos·arkts·navigation·navpathstack·api26
江华森7 小时前
把鸿蒙云手机调试的 5 个坑写成 Skill:12 项检查清单与报错决策树
harmonyos
木木子229 小时前
用华为云码道构建校园导航应用「新生通」:从需求分析到工程落地
华为·华为云·harmonyos
传奇开心果编程9 小时前
【Flutter踩坑排雷】第28课:平台目录缺失与修复坑|`flutter create` 的“重建”与“覆盖”边界
android·flutter·ios·移动开发·跨平台·dart·声明式ui
贾伟康10 小时前
HarmonyOS API 26 Navigation 自适应模式:mode、分栏切换与窗口断点治理
harmonyos·arkts·arkui·navigation·自适应布局
贾伟康10 小时前
【转盘决定吧|07】HarmonyOS ArkTS 6.0.2.130 趣味题库:场景分类、随机选择与详情页解耦
harmonyos·arkts·路由·数据建模·arkui
贾伟康10 小时前
【电路实验模拟|03】HarmonyOS ArkTS 交通灯实战:用定时状态切换红黄绿灯
harmonyos·arkts·canvas·arkui·交通灯
贾伟康1 天前
HarmonyOS API 26 嵌套 Navigation:getParent、父子栈边界与返回分发
harmonyos·arkts·navigation·嵌套路由·navpathstack
行者-全栈开发1 天前
【鸿蒙心迹】多线程并发实战——TaskPool 与 Worker 选型与踩坑(HarmonyOS 7.x)
华为·harmonyos·多线程并发·worker·taskpool·线程通信·harmonyos 并发