How to pass extras to a BroadcastReceiver

2019-04-14 15:05发布

问题:

I have a problem with passing extras to calls and catching them.

To make more clear what I intend to do:

Start a call and set extras for this intent. This is what my current code for this looks like:

Intent dialIntent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:" +  this.number));
dialIntent.putExtra("foo", true);
startActivity(dialIntent);

I implement a BroadcastReceiver to "catch" the call and want to access the extra I set when starting the activity. This is what I have in my manifest:

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

Unfortunately the intent that is passed to my CallReceiver is a NEW_OUTGOING_CALL intent.

I hope it's clear what I want to do and I hope it is possible.

Thanks in advance.

回答1:

Rather than using a BroadcastReciever here you actually want to make another 'default dialer' activity but this one will have a higher priority to ensure it will be launched before the native dialer app. Then you can alter the intent like this:

public void onCreate(Bundle b){
    Intent i = getIntent();
    //alter i here 
    startNextMatchingActivity(i);
}

hope this helps!