Detect Incoming call in Android programmatically

To detect incoming call and number in Android programmatically just follow below steps.

1) Add these permission in Manifest file.

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

2) Create a receiver class to handle incoming call & update it in manifest file

<receiver android:name="com.example.calllogreaderexample.TelephonyReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>

3) Code to check incoming call, ringing number & phone state in receiver class.

public class TelephonyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
try
{
if(intent != null && intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
{
//Toast.makeText(context, "Outgoign call", 1000).show();
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
else
{
//get the phone state
String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ? intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;
Bundle bundle = intent.getExtras();

if(newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//read the incoming call number
String phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("PHONE RECEIVER", "Telephone is now ringing " + phoneNumber);

}
else if(newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
//Once the call ends, phone will become idle
Log.i("PHONE RECEIVER", "Telephone is now idle");
}
else if(newPhoneState != null && newPhoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
//Once you receive call, phone is busy
Log.i("PHONE RECEIVER", "Telephone is now busy");
}

}

}
catch(Exception ee)
{
Log.i("Telephony receiver", ee.getMessage());
}
}