Tony
Tony

Reputation: 1185

I want my android app to start when a phone receives a call and to get the incoming phone number

I want my android app to start when a phone receives a call and to get the incoming phone number, i what to be able to put button on the screen of the incoming call and before that to be able to get the number calling, it would be of much help i anyone can refer me to some examples or materials. thx

Upvotes: 1

Views: 264

Answers (2)

denza
denza

Reputation: 1298

This will toast and log the incomming number...

  public class CallReceiveD extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    Bundle extras = intent.getExtras();
    if (extras != null) {
      String state = extras.getString(TelephonyManager.EXTRA_STATE);
      Log.w("DEBUG", state);
      if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String phoneNumber = extras
            .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);


       Toast toast= Toast.makeText(context,phoneNumber, Toast.LENGTH_LONG);toast.show();

        Log.w("DEBUG", phoneNumber);
      }
    }


}

      }

dont forget the manifest file < receiver android:name=".CallReceiveD"> < action android:name="android.intent.action.PHONE_STATE" />

        </intent-filter>
        </receiver>

Upvotes: 0

Philippe Girolami
Philippe Girolami

Reputation: 1876

You can setup a broadcastlistener in your AndroidManifet.xml You must setup your intent to listen for android.intent.action.PHONE_STATE Then you get the phone state from the intent with intent.getExtraString(TelephonyManager.EXTRA_STATE) . If it's OFFHOOK or RINGING then a call has come in and you can get the phone number from the intent with intent.getExtraString(TelephonyManager.EXTRA_INCOMING_NUMBER)

Upvotes: 2

Related Questions