I have a broadcast receiver which listens for all outgoing calls. In another activity I make an outgoing call. In my BC I want to be able to determine which calls were created in the activity, so I use putExtras()
to place a marker field when I'm making the call. Problem is, in the onReceive()
of the BC I don't see the extra data field at all (returns null).
Here is the relevant Activity code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appGlobal gState = (appGlobal)getApplicationContext();
dh = gState.getSqlDataHelper();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
phoneNumber = extras.getString("number");
}
makePhoneCall();
finish();
}
private void makePhoneCall()
{
if (phoneNumber.length() < 1) {
return;
}
String url = "tel:" + phoneNumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
intent.putExtra("number", "bla");
startActivity(intent);
}
And here is the relevant BC code:
public class CallMeNotServiceCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (intent.getStringExtra("number") != null)
{ Log.w("bla", "HAS KEY!!!"); }
...
Does this situation require a PendingIntent?
The official BroadcastReceiver API reference clearly states (3rd paragraph) :
So as Jason & HellBoy have suggested, instead of starting an Activity in makePhoneCall(), you send a Broadcast to your BroadcastReceiver, which in turn starts an Activity (only if it contains the marker extra of course)...