Simulator Screenshots

Broadcast Receiver For New Incomming Phone Call - Android Example

DOWNLOAD SOURCE CODE
 

In this android example we are showing ,when any new call event brodcasted ( When new call come ) then how to create receiver to read phone call states.

NOTE :  If you are new in android developement then first see this article Introduction To Broadcast Receiver Basics

 

AndroidManifest.xml File Details :

   1. Declare receiver in AndroidManifest

 

    <receiver android:name=".IncomingCall">   
                <intent-filter>
                   <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
        </receiver>

 

      2. Give read phone state permission in AndroidManifest

 

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

 

Complete code for AndroidManifest.xml File :

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidexample.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
       
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidexample.broadcastreceiver.BroadcastPhoneStates"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <receiver android:name=".IncomingCall">   
                <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
        </receiver>


    </application>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    
</manifest>

---------------------------------------------------------------------------------------------------------------------------------------

IncomingCall.java file details :

    I have made broadcast event reciever in this file

     1. Created class IncomingCall with extends BroadcastReceiver class

 

        public class IncomingCall extends BroadcastReceiver

 

      2.  Create method receiver()

 

               public void onReceive(Context context, Intent intent)

 

        3. Create listener to read phone state

 

               // TELEPHONY MANAGER class object to register one listner
                TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);

                        
                //Create Listner
                MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
                
                // Register listener for LISTEN_CALL_STATE
                tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

 

       4.  Create phone state listner class

           When new call come the onCallStateChanged method called

        private class MyPhoneStateListener extends PhoneStateListener {

                        public void onCallStateChanged(int state, String incomingNumber) {
                        
                           // Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);
                

                           // state = 1 means when phone is ringing
                            if (state == 1) {
                
                                String msg = " New Phone Call Event. Incomming Number : "+incomingNumber;
                                int duration = Toast.LENGTH_LONG;
                                Toast toast = Toast.makeText(pcontext, msg, duration);
                                toast.show();
                
                            }
                        }
                    }

 

Complete code for  IncomingCall.java file :

 

public class IncomingCall extends BroadcastReceiver {
    
    public void onReceive(Context context, Intent intent) {
        
    try {
               // TELEPHONY MANAGER class object to register one listner
                TelephonyManager tmgr = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                        
                //Create Listner
                MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
                
                // Register listener for LISTEN_CALL_STATE
                tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        
        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {
        
            Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

            if (state == 1) {

                String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(pcontext, msg, duration);
                toast.show();

            }
        }
    }
}
 

 

--------------------------------------------------------------------------------------------------------------------------------------------

 

How to test in simulator :

               Please see below video to test this example in simulator

 

Share this: