目录

Call Service Development

Scenario Description

You can implement the call service in either of the following ways:

Basic Concepts

Constraints

  1. The call service is available only on standard-system devices.
  2. An available SIM card must be present on the device.

Available APIs

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.

How to Develop

[h2]Making a Call by Using the makeCall API

  1. Import the call and observer modules.

  2. Invoke the hasVoiceCapability API to check whether the device supports the voice call function.

  3. Call the makeCall API to launch the dial screen and display the dialed number.

  4. (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));
         });
     }