How to Build Your Own Voice Call API in Android?[Update 2022]
Tags: voicecallapi, buildvoicecallapi, apivoicecall, voicecallapiandsdk
Published: 2021-04-17 08:42:09

<p><strong>Let’s Explore!</strong></p>
<p>Do you desire to build a voice call application in android?</p>
<p>Peeking around but still confused! Come on, Don’t lose hope!</p>
<p>This is your place to explore for the exceptional. Let’s dive deep and know how to build a simple android Voice calling app?</p>
<p>Before getting into this exciting journey, it’s salient to have some understanding about what the Voice Calling API is all about?</p>

<h2><strong>What is Voice Call API and How Does it Works?</strong></h2>

![voice call api diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pnpln9bgl44g8wkq3km.png)
<p>Well to start with, voice calling api is a tool that is used by the software developers to make and receive phone calls easily. In other words can say that it's the voice API that bridges the Public Switched Telephone Network (PSTN) and the applications get connected to the internet.</p>

<p>This API permits the software to be built into any existing application that includes iOS, Android, and web applications, easily.</p>

<p>Moving ahead as to how the task is being performed by this voice calling API. It is much essential to know about VoIP (Voice over internet protocol), to have a better understanding.&nbsp;</p>
<p>VoIP is a feature whose functionality is when added to an application, you can make and receive calls over the internet alone without interfacing with the PSTN.&nbsp;</p>
<p>Voice API has been built for a variety of uses from basic phone to phone calling, app to phone calling, conference calling, and much more. It is mainly to provide the developers a programmatic control over the calls.</p>

<h2><strong>Broad Vision on Building a Voice Call API</strong> &amp; SDK</h2>

<p>As we have already discussed about <a href="https://www.mirrorfly.com/voice-call-solution.php" target="_blank"><span style="color:#a30003" class="has-inline-color">voice call api</span></a>, Android places the calls via built-in applications that are easy to make and receive calls anywhere around. Even it allows us to place calls through the app.&nbsp;</p>

<p>While working in the way at the back of the screen everything is about programming. This is called the action intended to proceed further, which is nonetheless <span class="has-inline-color has-bright-blue-color"><strong>ACTION_CALL</strong>.</span></p>

<p>The intent object <span><strong>ACTION_CALL</strong></span> is when used and passed over proper data. It helps to launch the build-in phone call application to make the phone call. </p>

<p><strong>Let's know how?</strong></p>

<p><strong>STEP 1: Action intent to Make a call </strong> - To execute this process of calling the action used here is <strong><span class="has-inline-color has-bright-blue-color">ACTION_CALL</span></strong>. And the syntax used over here to add action call:</p>

<pre class="wp-block-preformatted">Intent myIntent = <strong>new</strong> Intent(Intent.ACTION_CALL); // this is for Action_call</pre>

<p>Other than this for the same process you can also make use of <strong><span class="has-inline-color has-bright-blue-color">ACTION_DIAL</span></strong>,</p>

<pre class="wp-block-preformatted">Intent myDialIntent = <strong>new</strong> Intent(Intent.ACTION_DIAL); // this is for Action_Dial</pre>

<p><strong>STEP 2:</strong> <strong>Data Intent to Make a call </strong>- For instance, if there is a need to make a call let us say -91-1234234679. We need to set the number using<span class="has-inline-color has-bright-blue-color"> </span><strong><span style="color:#002ea3" class="has-inline-color">setData</span></strong> in tel, this is the URL as been given below:</p>

<pre class="wp-block-preformatted">myIntent.setData(Uri.parse("tel:91-123-456-7890"));</pre>

<p>And in case if we want to use take the number from the user, then the <strong><span style="color:#002ea3" class="has-inline-color">getData</span></strong> would be something like the below from the user:</p>

<pre class="wp-block-preformatted">myIntent.setData(Uri.parse("tel:"+text_Phone.getText().toString()));startActivity(myIntent);</pre>

<h2 class="has-text-align-left"><strong>Explore Voice Calling APIs Most Common Calling Scenarios</strong></h2>

<p>Let's have a look at some of the common scenarios, How does <a href="https://blog.contus.com/clubhouse-drop-in-audio-chat-app" target="_blank"><span style="color:#a32200" class="has-inline-color">voice call apps</span></a> use the API and have control over them?</p>

<p><strong>These three scenarios can get the most</strong>:</p>

<ol>
<li>Answer the call</li>
<li>Accommodating a call</li>
<li>Ending a Call</li>
</ol>

<h3>1.<strong>Answer the Call</strong></h3>

<p>Before making a call from your android mobile you must ensure that there is no call being executed at that particular moment.&nbsp;</p>

<p>This is so as because the telecom subsystem provides certain constraints when it comes to active calls in other apps. </p>

<p><strong>Let's consider the two cases:</strong></p>

<p><strong>I. No other app are actively on call:</strong></p>

<ul><li>Once your App receives a call</li><li>Call notification will be sent via <strong><span style="color:#2614e5" class="has-inline-color">addNewIncomingCall(PhoneAccountHandle, Bundle)</span></strong> to the telecom subsystem.</li><li>This subsystem will then bind to your app's <strong><span style="color:#002ea3" class="has-inline-color">ConnectionService</span></strong> implementation. With that a new instance of Connection class takes place using <span style="color:#002ea3" class="has-inline-color"><strong>onCreateIncomingConnection</strong></span><strong><span style="color:#f80dcd" class="has-inline-color">(PhoneAccountHandle, ConnectionRequest)</span></strong><span style="color:#002ea3" class="has-inline-color">.</span></li><li>Later on this telecom subsystem informs the app and shows the incoming calls to users <strong><span style="color:#f80dcd" class="has-inline-color">onShowIncomingCallUi()</span>.</strong></li><li>Once the user accepts the incoming call, it gets set to <strong><span style="color:#f80dcd" class="has-inline-color">setActive()</span></strong>. If not, then get <strong><span style="color:#002ea3" class="has-inline-color">REJECTED</span></strong> with <strong><span style="color:#f80dcd" class="has-inline-color">setDisconnected(DisconnectCause)</span></strong> execution, followed by the <span style="color:#002ea3" class="has-inline-color"><strong>Destroy()</strong> </span>method.</li></ul>

<p><strong>II. Active Calls that other App has can't be kept on Hold:</strong></p>

<p>Sometimes, it does happen that some important call needs to be attended and can't be put on hold. In such cases the things are as follows:</p>

<ul><li>As soon as an incoming call receives a new call over the voice call app.</li><li>The notification will be sent to the telecom subsystem as Call <strong>a<span style="color:#002ea3" class="has-inline-color">ddNewIncomingCall</span><span style="color:#f80dcd" class="has-inline-color">(PhoneAccountHandle, Bundle)</span></strong></li><li>This subsystem binds to your app's <strong><span style="color:#f80dcd" class="has-inline-color">ConnectionService</span></strong> implementation. It later on requests a new instance over the connecting objects that have been used, <strong><span style="color:#002ea3" class="has-inline-color">onCreateIncomingConnection</span><span style="color:#f80dcd" class="has-inline-color">(PhoneAccountHandle, ConnectionRequest)</span></strong>.</li><li>Now, it's the telecom subsystem that displays the incoming call on UI.</li><li>If the user tends to accept the call, this subsystem calls upon <span style="color:#002ea3" class="has-inline-color"><strong>onAnswer()</strong> </span>method. Thus, indicates the telecom subsystem as the setted&nbsp; call is as <strong><span style="color:#002ea3" class="has-inline-color">setActive()</span></strong>.</li><li>If in turn the user rejects the call, this subsystem calls upon the <span style="color:#002ea3" class="has-inline-color"><strong>onReject()</strong> </span>method. Then passes on the message as <strong><span style="color:#002ea3" class="has-inline-color">REJECTED</span></strong> by setting the call as <strong><span style="color:#002ea3" class="has-inline-color">setDisconnected</span><span style="color:#f80dcd" class="has-inline-color">(DisconnectCause)</span></strong>.</li><li>And the later will be called as <strong><span style="color:#002ea3" class="has-inline-color">Destroy()</span></strong> method</li></ul>

<p>Now, accommodating a call or placing a call is all about something that we are supposed to handle when the call cannot be connected due to some constraints that are imposed by the <a href="https://en.wikipedia.org/wiki/Business_Process_Framework_(eTOM)">telecom framework</a>.</p>

<p><strong>In general, if we want to place a call we must follow the below things</strong>:</p>

<ol><li>First must initiate the call via application.</li><li>Use the method <strong><span style="color:#002ea3" class="has-inline-color">placeCall</span><span style="color:#f80dcd" class="has-inline-color">(call, bundle)</span></strong> and then intimate the telecom subsystem about the new call. Now the rest of parameter would be as follows:</li></ol>

<ul><li>Here, the <strong>Uri</strong> parameter represents the address of the callee. For numbers must use <strong>tel:</strong></li><li>Well, the parameters give information about the application. All this is done by adding the <strong><span style="color:#f80dcd" class="has-inline-color">phoneAccountHandle</span> </strong>object of the app to <strong><span style="color:#002ea3" class="has-inline-color">EXTRA_PHONE_ACCOUNT_HANDLE</span></strong> extra.</li><li>These parameters can also specify if the bundle includes video by executing <strong><span style="color:#002ea3" class="has-inline-color">EXTRA_START_CALL_WITH_VIDEO_STATE </span></strong>.</li><li>On the whole, this telecom subsystem binds to the voice call app's <strong>ConnectService</strong> implementation.</li></ul>

<p><strong>i) If in case the app is unable to call, the subsystem invokes: <span style="color:#002ea3" class="has-inline-color">onCreateOutgoingConnectionFailed</span><span style="color:#f80dcd" class="has-inline-color">(PhoneAccountHandle, ConnectionRequest)</span></strong>. It generates a message to the application as the call can't be set.</p>

<p><strong>ii) But if the app is able to make a call, then it will invoke: <span style="color:#002ea3" class="has-inline-color">onCreateOutgoingConnectionFailed</span><span style="color:#f80dcd" class="has-inline-color">(PhoneAccountHandle, ConnectionRequest)</span></strong> specifying the application as the call can't be set at the first place instantly make the update by activating the call, invoke <strong>setActive()</strong> to inform telecom subsystem about the same.</p>

<p>Once the call get connected, we would need to end the call at some stage, in that place to end the session, we need to do the following,</p>

<ul><li>Call the method <strong><span style="color:#002ea3" class="has-inline-color">setDisconnected</span><span style="color:#f80dcd" class="has-inline-color">(DisconnectCause)</span></strong>.</li><li>If in case the call gets terminated by the user <strong><span style="color:#002ea3" class="has-inline-color">LOCAL</span> </strong>parameter will be executed. Similarly, the parameter <strong><span style="color:#002ea3" class="has-inline-color">REMOTE</span> </strong>will be executed on if the call is disconnected by a third party.</li><li>Once done the entire process is followed by the parameter <strong><span style="color:#002ea3" class="has-inline-color">destroy()</span></strong>.</li></ul>

<h2><strong>Steps to Build Your Voice Call API in Android</strong></h2>

<p><strong>Step 1 : </strong>We have to begin with creating a new project by filling in the details. Then that needs to be opened by <strong><span style="color:#002ea3" class="has-inline-color">activity_main,xml file</span></strong>, and thus defines the layout as given below:</p>
<pre class="wp-block-preformatted">
xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:orientation="vertical"    android:layout_width="match_parent"
  android:layout_height="match_parent">
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginLeft="100dp"
      android:layout_marginTop="100dp"
      android:fontFamily="@font/aclonica"
      android:text="DataFlair "
      android:textColor="@color/colorPrimaryDark"
      android:textSize="50dp" />
      android:id="@+id/fstTxt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="100dp"
      android:layout_marginTop="100dp"
      android:fontFamily="@font/acme"
      android:text="Please Enter the Mobile No"
      android:textColor="#0F8B80"
      android:textSize="20dp" />
      android:id="@+id/mblTxt"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="100dp"
      android:ems="10"
      android:hint="Enter the number here"
      android:textColor="#84E2DB"
      tools:layout_marginTop="20dp"></EditText>
      android:id="@+id/btnCall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="150dp"
      android:layout_marginTop="20dp"
      android:text="Dial" />
</pre>

<p><strong>Step 2: </strong>Now this is followed by coding as in <strong><span style="color:#002ea3" class="has-inline-color">mainActivity.java file</span></strong>. Thus follows:</p>

<pre class="wp-block-preformatted">
package com.DataFlair.callexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public <strong>class</strong> MainActivity <strong>extends</strong> AppCompatActivity {
&nbsp;&nbsp;private EditText text_Phone;
&nbsp;&nbsp;private Button b_utton;
&nbsp;&nbsp;@Override
&nbsp;&nbsp;protected <strong>void</strong> onCreate(Bundle savedInstanceState) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(savedInstanceState);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setContentView(R.layout.activity_main);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text_Phone = (EditText) findViewById(R.id.mblTxt);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mybutton = (Button) findViewById(R.id.btnCall);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mybutton.setOnClickListener(<strong>new</strong> View.OnClickListener() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public <strong>void</strong> onClick(View v) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callPhoneNumber();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;}
&nbsp;&nbsp;@Override
&nbsp;&nbsp;public <strong>void</strong> onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if</strong> (requestCode == 101) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if</strong> (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callPhoneNumber();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;public <strong>void</strong> callPhoneNumber() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>try</strong> {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if</strong> (Build.VERSION.SDK_INT &gt; 22) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>if</strong> (ActivityCompat.checkSelfPermission(<strong>this</strong>, Manifest.permission.CALL_PHONE) !=PackageManager.PERMISSION_GRANTED) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ActivityCompat.requestPermissions(MainActivity.this, <strong>new</strong> String[]{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Manifest.permission.CALL_PHONE}, 101);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>return</strong>;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent callIntent = <strong>new</strong> Intent(Intent.ACTION_CALL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callIntent.setData(Uri.parse("tel:" + text_Phone.getText().toString()));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startActivity(callIntent);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <strong>else</strong> {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent callIntent = <strong>new</strong> Intent(Intent.ACTION_CALL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callIntent.setData(Uri.parse("tel:" + text_Phone.getText().toString()));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startActivity(callIntent);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <strong>catch</strong> (Exception ex) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre>

<p><strong>Step 3:</strong> This is followed by <span style="color:#002ea3" class="has-inline-color"><strong>Manifest.xml</strong></span> file:</p>


<pre class="wp-block-preformatted"> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.DataFlair.callexample">
  uses-permission android:name="android.permission.CALL_PHONE" />
  application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      activity android:name=".MainActivity">
          intent-filter
              action android:name="android.intent.action.MAIN" 
              category android:name="android.intent.category.LAUNCHER" 
         intent-filter
      activity
      meta-data
          android:name="preloaded_fonts"
          android:resource="@array/preloaded_fonts" 
  application
manifest
</pre>

<p><strong>Step 4: </strong>As the final step, the application needs to be commanded as <strong>“<span style="color:#002ea3" class="has-inline-color">run.”</span></strong></p>

<p>a) The outcome of the application will be somewhat like this</p>

<div class="wp-block-image">![build a voice call api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3qak0rh0ggwg909cfyn.jpg)</div>

<p>b) Once done, need to enter the phone number</p>

<div class="wp-block-image">
![voice calling api & sdk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e9z1guofz0yirrfyqwy.jpg)</div>

<p>c) As soon as the phone number has been entered, it will ask for the permission to move forward</p>

<div class="wp-block-image">
![voice call api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gndrxsya2jnugps6uuph.jpg)</div>

<p>d) Once, the above steps are completed automatically the call would be placed</p>

<div class="wp-block-image">
![voice api for android](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcekg5fme35bii3gnynq.jpg)</div>

<p><strong>Epilogue</strong></p>

<p>Now you must have gotten a better insight about the entire process with a detailed explanation as how to build a voice call API for any Android studio. With this now it's your turn to implement the same and get to explore the outcome with flying colors.</p>

<p>But still, if you think you need some clarification over the same. Let us know in the comment section to guide you further with pleasure!&nbsp;</p>

<p><strong><span class="has-inline-color has-dark-brown-color">Code &amp; Image References Taken From:</span></strong></p>

<p>1. <a href="https://data-flair.training/blogs/calling-app-in-android/" target="_blank">Data Flair.Training</a> </p>

<p>2. <a href="https://www.c-sharpcorner.com/article/how-to-create-a-calling-application-in-android-using-android-studio/" target="_blank">C Sharpcorner.com</a> </p>

<p>3. <a href="https://developer.android.com/guide/topics/connectivity/telecom/selfManaged" target="_blank">Developer Android.com</a> </p>

<p>4. <a href="https://www.dbswebsite.com/blog/trends-in-voice-search/#:~:text=Current%20statistics%20show%20that%2041,voice%20technology%20on%20their%20device&amp;text=." target="_blank">DBS Website.com </a></p>

<p>5. <a href="https://professional-blogger.medium.com/integrate-a-voice-call-api-in-various-industries-in-app-communication-system-e3f2a1ffcc7">Integrate Voice Call API & SDK</a></p>