Android Phone Calls With Examples
Android Phone Calls With Examples
In android, we can easily make a phone call from our android applications by invoking built-in
phone calls app using Intents action (ACTION_CALL).
Generally, the Intent object in android with proper action (ACTION_CALL) and data will help us
to launch a built-in phone calls app to make a phone calls in our application.
In android, Intent is a messaging object which is used to request an action from another app
component such as activities, services, broadcast receivers, and content providers.
To make a phone call using Intent object in android application, we need to write the code like as
shown below.
Create a new android application using android studio and give names as PhoneCallExample.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Mobile No"
/>
<EditText
android:id="@+id/mblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10">
</EditText>
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Call" />
</LinearLayout>
Now open our main activity
file MainActivity.java from \src\main\java\com.tutlane.phonecallexample path and write the
code like as shown below
MainActivity.java
package com.sjkpgm.phonecallexample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callPhoneNumber();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions
, int[] grantResults)
{
if(requestCode == 101)
{
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callPhoneNumber();
}
}
}
public void callPhoneNumber()
{
try
{
if(Build.VERSION.SDK_INT > 22)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.
CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new Stri
ng[]{Manifest.permission.CALL_PHONE}, 101);
return;
}
}
else {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + txtPhone.getText().toString
()));
startActivity(callIntent);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
If you observe the above code we are adding Runtime permissions to make sure our application
work in both old / latest android OS versions and we used Intent action (ACTION_CALL) to make
a phone call on button click using default phone calls app.
Now open android manifest file (AndroidManifest.xml) and write the code like as shown below
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.phonecallexample">
<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.LAUNCHE
R" />
</intent-filter>
</activity>
</application>
</manifest>
If you observe above AndroidManifest.xml file we added a CALL_PHONE permission in
manifest file.
Once we enter the phone number and click on the Call button, it will invoke built-in phone calls
app and it will make a call to respective phone numbers like as shown below.
This is how we can make phone calls using Intents in android applications based on our
requirements.