Search...

Tuesday, March 20, 2012

How to Retrieve incoming call’s phone number in Android?

Step1: Add Below code in your manifest file


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

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />


Step2: Create class to receive Broadcast message




public class CallBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   
 String action = intent.getAction();

           if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
              if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                                  TelephonyManager.EXTRA_STATE_RINGING)) {
                  //Incoming call
               doSomething(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
              }
}



No comments:

Post a Comment