Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Android Phone Calls
2.1.
Step 1: Permission for making a call
2.2.
Step 2: Add a button to make a call
2.3.
Step 3: Add onClick listener for this button
2.4.
Output
3.
FAQs
4.
Key Takeaways  
Last Updated: Mar 27, 2024
Easy

Android Phone Calls

Author Pradeep Kumar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will see how we can make a call in an android application. It can be done by using implicit intents with a specific action. We will discuss the step-by-step process of creating an intent to make a phone call by an android application in this article.

In case you are not familiar with intents, I suggest you go through this article before moving forward. 

You can also read about, android operating system

Android Phone Calls

In some cases, we may need to make a phone call using our program; you can accomplish that by using intent with the action ACTION CALL. The intent is a simple message object used to communicate with Android components such as activities, services, broadcast receivers, and content providers. In this case, we are using an intent to make a phone call. Let's discuss the process through an example:

Step 1: Permission for making a call

The first step is to add a user-permission in the AndroidManifest.xml to make a phone call. 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidphonecalls">

    <!--    Permission to make a call-->
    <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/Theme.AndroidPhoneCalls">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 2: Add a button to make a call

Now, add a button in the activity_main.xml, which on pressed will make a phone call to the specified number. 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--    Code to add a button-->
    <Button
        android:id="@+id/call_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="151dp"
        android:layout_marginBottom="169dp"
        android:text="Call"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
   
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Add onClick listener for this button

Now, we will add an on-click listener for this button in the MainActivity.kt file. We will first check if the app has permission to make phone calls or not. If the app doesn't have permission, the makeCall function will just return without making the call. The call will only go through if the app has permission to do so.

MainActivity.kt

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import android.content.Intent
import android.net.Uri


class MainActivity : AppCompatActivity() {
    private lateinit var button: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       
        // Getting the button with call_button id
        button = findViewById<Button>(R.id.call_button)
           
        // Add a onClickListener for the button
        button!!.setOnClickListener{
            makeCall()
        }
    }
    // Function to make a call
    private fun makeCall() {
        // Intent with action ACTION_CALL
        val callIntent = Intent(Intent.ACTION_CALL)
        // parsing the number to call
        callIntent.data = Uri.parse("tel:0377778888")
        // check if the app has permission to make a call
        if (ActivityCompat.checkSelfPermission(
                this@MainActivity,
                Manifest.permission.CALL_PHONE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            //return if the app does not have permission to make a phone call
            return
        }
        // make the phone call
        startActivity(callIntent)
    }
}

Output

Upon clicking the call button, the second screen will pop up, and the call will go through.

Note: For the call to go through successfully, your app must have permission to make a phone call. You can do so by going to the settings of your phone/emulator and then choosing this app and granting it permission to make a call. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

1. How can we monitor the change in telephony states of a device?
Ans: You can do so by using PhoneStateListener and TelephonyManager class.
 

2. What is the use of defining user permission in the AndroidManifest.xml file?
Ans: By defining user permission in the file, your device gets to know about the permissions that your app will need. When you go to the settings and check for the app permissions of this app, you will find a request for this specific permission.
 

3. How does this app know which number to call on the press of the call button?
Ans: The number to call on the press of the button is specified in the MainActivity.kt file. You can change this number as per your liking or even take the number as input from the user. 

Key Takeaways  

In this article, we have extensively discussed the process of making a phone call using intent in an android application. We hope that this blog has helped you enhance your knowledge regarding Android Phone Calls, and to learn more about the type of intents, check out our article on Android Intent Types. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Sending SMS in Android
Next article
Android Camera
Live masterclass