3

When looking at Android source, I observe two common methods for getting the Bundle containing the result's extra data.

A. Calls getResultsExtra()

private class StatusBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = getResultExtras(true);
    }
}

B. Calls intent.getExtras()

private class StatusBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
    }
}

Why would you choose one method over the other method? Are there certain situations where one is preferred over the other method? Are they equivalent?

GrandAdmiral
  • 1,348
  • 2
  • 24
  • 52

2 Answers2

3

The "BroadcastReceiver.getResultExtras" method makes sense when you are sending ordered broadcasts with the "Context.sendOrderedBroadcast", because you can modify the result set to be sent to the next broadcast receiver (remember that when you send an ordered broadcast, the receivers are called in a synchronous mather, rather that the asynchronous mather of the common broadcast "Context.sendBroadcast"). With that in mind, imagine that you have 3 receivers and you sent an ordered broadcast to them (assuming you put the priorities with the broadcast 1 as the first and then), then receiver 1 can get the result set with the "BroadcastReceiver.getResultExtras" method and add some values, that values will be seen by the receiver 2 when the broadcast reach to it and so on. The "Intent.getExtras()" will only give you the extra values of the Intent your broadcast receiver has.

josemgu91
  • 719
  • 4
  • 8
2

getResultExtras() is used like this :

Bundle results = getResultExtras(true);
results.putString("someTag", "someValue");

to share information/values between different receivers

So, if you'll put some value or change the returned map from the getResultExtra() in some BroadcastReceiver , it'll be sent to the next receiver as per the docs

Retrieve the current result extra data, as set by the previous receiver. Any changes you make to the returned Map will be propagated to the next receiver.

on the other hand intent.getExtras() returns the values that were set in the intent extras using intent.putExtra() while starting the broadcast, for example this :

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("com.yourpackage.BroadcastReceiver");
intent.putExtra("someTag", "someValue");
sendBroadcast(intent);
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • So if I am not changing any of the values and am only interested in reading the values, the two are equivalent? – GrandAdmiral Jun 28 '16 at 20:50
  • ah, no. if bool in getResultExtras(bool) is set to true then you get the values(map) sent from the previous receiver,(new empty map is returned if the previous map is null) and if set to false it returns a null map. and intent.getExtras() will only have the values that were set when the broadcast was started using `sendBroadcast(intent);`. So the getResultExtras(bool) won't have same values that intent.getExtras() has! – Ashish Ranjan Jun 28 '16 at 20:57