Monday, May 7, 2012

Tracking call from a particular number in Android


Tracking call from a particular number in Android

Here manifest looks like,
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<receiver
android:name="ReceiveCallToMute"
android:permission="android.permission.READ_PHONE_STATE" >
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" >
    </action>
</intent-filter>
</receiver>

And broadcast receiver will invoked when a call comes,
For a call, there are three states,
1. Ringing
2. Idle
3. Offhook

public class ReceiveCallToMute extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
  Bundle b=intent.getExtras();
  if(b!=null){
  String state=b.getString(TelephonyManager.EXTRA_STATE);
  String number=b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
  if(number.indexOf("9447024365") != -1) {
  if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
  
  }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
  
  }else if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){  
  
  }
  }
  }
}
}