2

I have an activity that makes a call:

public void call(String number) {
    Intent intent = new Intent("android.intent.action.CALL");
    intent.setData(Uri.parse("tel:" + number));
    intent.putExtra("MY_EXTRA", "Hello");
    startActivity(intent);
    finish();
}

I set up a broadcast receiver for calls:

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("Extra", intent.getStringExtra("MY_EXTRA", "Default"));
        }
    }
}

And registered it:

<receiver android:name=".OutgoingCallReceiver">
    <intent-filter android:priority="-1">
        <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

The broadcast receiver works. But regardless of whether it receives the event from the dialer or from my Activity, there is no extra.

Why are the extras not going through?

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75

3 Answers3

1

It doesn't work like that. You can't add arbitrary extras to the "ACTION_CALL" Intent and expect that these extras will show up in the "NEW_OUTGOING_CALL" Intent that is broadcast. There isn't any way that you can add your own extras to the "NEW_OUTGOING_CALL" broadcast Intent. You only get the extras that the dialer puts into the Intent (like the phone number).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Why? Is there a reason it's stripping values? Is there any kind of source you could show me on this? – Anubian Noob Jul 02 '15 at 14:07
  • It isn't "stripping values". The 2 `Intent`s are completely different. first one is an `Intent` used to start an `Activity`. This launches the dialer application. The second one is a broadcast `Intent` which is sent by a completely different part of the system (the underlying call framework). – David Wasser Jul 02 '15 at 14:28
  • So as a general rule of thumb sending a broadcast intent strips extras? And sending an intent to `android.intent.action.CALL` is a broadcast intent? – Anubian Noob Jul 02 '15 at 14:43
  • No. Extras work fine in broadcast `Intent`s. You are missing the point. We are talking about 2 separate `Intent`s. Look at your code. You are calling `startActivity()` with the `Intent` containing `android.intent.action.CALL`. This starts an `Activity` (usually the system dialer, but can be other 3rd party apps like Skype or whatever). At some point the underlying call framework decides that it needs to make the call and it then sends an ordered broadcast `Intent` with `android.intent.action.NEW_OUTGOING_CALL`. – David Wasser Jul 02 '15 at 15:45
  • Your random extras that you stick in the CALL `Intent` are ignored by the dialer, because it doesn't expect them and doesn't know what to do with them. They don't get copied into the broadcast `Intent` because nobody knows about them and that isn't the way this works. – David Wasser Jul 02 '15 at 15:46
0

Everything seems ok but as i can see:

http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

intent.getStringExtra("MY_EXTRA")

takes one argument, there is no default value like for getIntExtra() etc...

gaugeinvariante
  • 175
  • 1
  • 10
0

The intent you send never gets caught by the broadcast reciever. It triggers the call, which sends it's own broadcast intent.

The caller disregards any other extras you send to it; it doesn't know what to do with them.

You could try using SharedPreferences to save state between the Activity sending the intent and the BroadcastReciever.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75