HarmonyOS NEXT Practical: Making a Phone Call
Objective: Enter a phone number and make a call. Knowledge points: The Telephone Kit provides a series of APIs to assist developers in developing communication applications, including: Call module (making phone calls): The system application can directly make phone calls and display the call on the application interface; The third-party application can pull up the system phone application, jump to the dialing interface, and thus achieve the function of making phone calls. For details, please refer to the development guide for making phone calls. In addition, the application can also use the call module to format phone numbers, determine if they are emergency numbers, and other functions. For details, please refer to the @ohos.telephony.call API reference. SMS module (SMS service): The application can realize the function of creating and sending SMS messages. For details, please refer to the SMS development guide. In addition, the application can also achieve functions such as obtaining and setting the address of the SMS service center, and checking whether the current device has the ability to send and receive SMS. For details, please refer to the @ohos.telephony.sms API reference. Radio module (network search): The application can call the API to obtain the current registered network name, network service status, and signal strength related information. For details, please refer to @oho.telephony. Radio API reference. Data module (cellular data): Cellular data is a type of wireless communication technology standard that uses packet switch technology for data transmission and exchange. It can provide voice, data, video and image services for mobile devices, and is often used to support users in using applications on smart devices and browsing web pages on mobile networks. For more information, please refer to the @oho.telephony.data API reference. SIM module (SIM card management): Applications can call APIs to obtain SIM card related information, such as service provider, ISO (International Organization for Standardization) country code, and home PLMN (Public Land Mobile Network) number. For details, please refer to @ohs.telephony. sim API reference. Use makeCall to make phone calls Import the call and observer modules. Call hasVoiceCapability to confirm if the current device supports dialing. Call the makeCall interface to jump to the dialing interface and display the number to be dialed. Actual combat:CallPhoneDemoPage import { call, observer } from '@kit.TelephonyKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct CallPhoneDemoPage { @State message: string = 'Hello World'; @State phone: string = '' build() { Column({ space: 10 }) { Text('拨打电话Demo') TextInput({ placeholder: '请输入要拨打的电话' }) .type(InputType.PhoneNumber) .onChange((value) => { this.phone = value }) Button('拨号') .onClick(() => { // 调用查询能力接口 let isSupport = call.hasVoiceCapability(); if (isSupport) { // 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码 call.makeCall(this.phone, (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)); }); } }) } .height('100%') .width('100%') .padding({ left: 20, right: 20 }) } }

Objective: Enter a phone number and make a call.
Knowledge points:
The Telephone Kit provides a series of APIs to assist developers in developing communication applications, including:
- Call module (making phone calls): The system application can directly make phone calls and display the call on the application interface; The third-party application can pull up the system phone application, jump to the dialing interface, and thus achieve the function of making phone calls. For details, please refer to the development guide for making phone calls. In addition, the application can also use the call module to format phone numbers, determine if they are emergency numbers, and other functions. For details, please refer to the @ohos.telephony.call API reference.
- SMS module (SMS service): The application can realize the function of creating and sending SMS messages. For details, please refer to the SMS development guide. In addition, the application can also achieve functions such as obtaining and setting the address of the SMS service center, and checking whether the current device has the ability to send and receive SMS. For details, please refer to the @ohos.telephony.sms API reference.
- Radio module (network search): The application can call the API to obtain the current registered network name, network service status, and signal strength related information. For details, please refer to @oho.telephony. Radio API reference.
- Data module (cellular data): Cellular data is a type of wireless communication technology standard that uses packet switch technology for data transmission and exchange. It can provide voice, data, video and image services for mobile devices, and is often used to support users in using applications on smart devices and browsing web pages on mobile networks. For more information, please refer to the @oho.telephony.data API reference.
- SIM module (SIM card management): Applications can call APIs to obtain SIM card related information, such as service provider, ISO (International Organization for Standardization) country code, and home PLMN (Public Land Mobile Network) number. For details, please refer to @ohs.telephony. sim API reference.
Use makeCall to make phone calls
- Import the call and observer modules.
- Call hasVoiceCapability to confirm if the current device supports dialing.
- Call the makeCall interface to jump to the dialing interface and display the number to be dialed.
Actual combat:CallPhoneDemoPage
import { call, observer } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct CallPhoneDemoPage {
@State message: string = 'Hello World';
@State phone: string = ''
build() {
Column({ space: 10 }) {
Text('拨打电话Demo')
TextInput({ placeholder: '请输入要拨打的电话' })
.type(InputType.PhoneNumber)
.onChange((value) => {
this.phone = value
})
Button('拨号')
.onClick(() => {
// 调用查询能力接口
let isSupport = call.hasVoiceCapability();
if (isSupport) {
// 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码
call.makeCall(this.phone, (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));
});
}
})
}
.height('100%')
.width('100%')
.padding({ left: 20, right: 20 })
}
}