You can implement the call service in either of the following ways:
Call status code
A code used to report the current call status to the application, so that the application can then take appropriate logic processing. For example, if there is no ongoing call, the application allows you to make a new call.
| Name | Value | Description |
|---|---|---|
| CALL_STATE_UNKNOWN | -1 | The call status fails to be obtained and is unknown. |
| CALL_STATE_IDLE | 0 | No call is in progress. |
| CALL_STATE_RINGING | 1 | The call is in the ringing or waiting state. |
| CALL_STATE_OFFHOOK | 2 | At least one call is in dialing, active, or on hold, and no new incoming call is ringing or waiting. |
To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see API Reference.
| Name | Description |
|---|---|
| hasVoiceCapability(): boolean; | Checks whether the voice call function is supported. The default value is false. - true: The voice call function is supported. - false: The voice call function is not supported. |
| makeCall(phoneNumber: string, callback: AsyncCallback<void>): void; | Redirects to the dial screen and displays the called number. |
The observer module provides the functions of subscribing to and unsubscribing from the call service status. For details about the APIs, see API Reference.
| Name | Description |
|---|---|
| on(type: 'callStateChange', options: { slotId: number }, callback: Callback<{ state: CallState, number: string }>): void; | Listens to call status changes. |
Import the call and observer modules.
Invoke the hasVoiceCapability API to check whether the device supports the voice call function.
Call the makeCall API to launch the dial screen and display the dialed number.
(Optional) Register the observer for call service status changes.
// Import the required modules.
import { call, observer } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';
// Check whether the voice call function is supported.
let isSupport = call.hasVoiceCapability();
if (isSupport) {
// If the voice call function is supported, the user will be redirected to the dial screen and the dialed number is displayed.
// The tel URI format is supported since API version 15, for example, tel:13xxxx.
call.makeCall("13xxxx", (err: BusinessError) => {
if (!err) {
console.info("make call success.");
} else {
console.error("make call fail, err is:" + JSON.stringify(err));
}
});
// (Optional) Register the observer for call service status changes.
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.info("call state change, data is:" + JSON.stringify(data));
});
}