Native Mobile App Development With Java

Android Detect Phone Call BroadcastReceiver example

15-Sep-2024

Learn how to detect incoming and outgoing phone calls in Android apps using BroadcastReceiver with Java


 Detect Phone Call BroadcastReceiver


 Create A Class PhoneCallReceiver class and extends BroadcastReceiver





package com.example.phonecallbroadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class PhoneCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
String phoneCallStatus = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

if (TelephonyManager.EXTRA_STATE_RINGING.equals(phoneCallStatus)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (incomingNumber != null) {
Log.d("PhoneCallReceiver", "Incoming call from : " + incomingNumber);
Toast.makeText(context, "Incoming call from : " + incomingNumber, Toast.LENGTH_SHORT).show();
} else {
Log.d("PhoneCallReceiver", "Incoming call from : " + "unknown");
Toast.makeText(context, "Incoming call from : " + "unknown", Toast.LENGTH_SHORT).show();
}
}
}
}
}


 Take Permission From the user. If permission is not given you can not detect the phone call





package com.example.phonecallbroadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED )
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CALL_LOG}, PERMISSION_REQUEST_CODE);
}
}

}


Then Change AndroidManifest.xml (Give Permissions and Add receiver in the application )






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

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PhoneCallBroadcastReceiver"
tools:targetApi="31">
<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>
<receiver android:name=".PhoneCallReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>

</manifest>


Now run the application and when an incoming call is on your device the Toast will show the incoming call number.

Comments