Incoming Phone Call Receiver - Android Broadcast Receiver 

🎯 Overview Broadcast Receiver

🎯 Broadcast Receiver Source Code

public class MyReceiver extends BroadcastReceiver {


        public void onReceive(final Context context, Intent intent) {

            Log.i("MyReceiver", "call state changed.... ");

//Tracking 2nd sim Mobile number

            String second_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            String second_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            Log.i("MyReceiver", "call state changed.... "+second_number);

            Log.i("MyReceiver", "call state changed.... "+second_state);

            TelephonyManager mtelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

            mtelephony.listen(new PhoneStateListener(){


                public void onCallStateChanged(int state, String incomingNumber) {

                    Log.i("MyReceiver", "call state changed.... "+state);

                    super.onCallStateChanged(state, incomingNumber);

                    switch (state) {

                        case TelephonyManager.CALL_STATE_RINGING:

                            Log.d("MyReceiver", "I'm in " + state + " and the number is " + incomingNumber);









🎯 How Code Works?

The code consists of a BroadcastReceiver called MyReceiver that is responsible for receiving and handling incoming phone call events. When an incoming call is received, the onReceive() method of the MyReceiver class is triggered. The onReceive() method is overridden to provide custom behavior when a phone call state changes.

Inside the onReceive() method, the code logs a message indicating that the call state has changed. It then retrieves the second SIM's mobile number and the call state from the received intent using the TelephonyManager.EXTRA_INCOMING_NUMBER and TelephonyManager.EXTRA_STATE extras, respectively.

After logging the call state and number, the code creates an instance of TelephonyManager to access telephony-related information and registers a PhoneStateListener to listen for changes in the call state.

Inside the PhoneStateListener's onCallStateChanged() method, the code logs the updated call state and the incoming phone number when the call state is CALL_STATE_RINGING. This allows tracking of the phone call's state and associated number.

The code also includes the necessary setup in the activity and manifest files to register the MyReceiver class as a receiver for the ACTION_PHONE_STATE_CHANGED intent.

🎯 From Activity

MyReceiver myReceiver = new MyReceiver();

IntentFilter intentFilter = new IntentFilter();



registerReceiver(myReceiver, intentFilter);

🎯 Manifest.XML

<receiver android:name=".MyReceiver" >


        <action android:name="android.intent.action.PHONE_STATE" />



🎯 Permissions

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

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