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.