Chapter 29 Broadcast Intents
Modify VisibleFragment to tell the sender of SHOW_NOTIFICATION whether the notification should be
posted. This information will also be sent to any other broadcast receivers along the chain.
Listing 29.12 Sending a simple result back (VisibleFragment.java)
public abstract class VisibleFragment extends Fragment {
private static final String TAG = "VisibleFragment";
...
private BroadcastReceiver mOnShowNotification = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getActivity(),
"Got a broadcast:" + intent.getAction(),
Toast.LENGTH_LONG)
.show();
// If we receive this, we're visible, so cancel
// the notification
Log.i(TAG, "canceling notification");
setResultCode(Activity.RESULT_CANCELED);
}
};
...
}
Because all you need to do is signal yes or no here, you only need the result code. If you needed to
return more complicated data, you could use setResultData(String) or setResultExtras(Bundle).
And if you wanted to set all three values, you could call setResult(int, String, Bundle). Once
your return values are set, every subsequent receiver will be able to see or modify them.
For those methods to do anything useful, your broadcast needs to be ordered. Write a new method to
send an ordered broadcast in PollService. This method will package up a Notification invocation
and send it out as a broadcast. Update onHandleIntent(...) to call your new method and, in turn, send
out an ordered broadcast instead of posting the notification directly to the NotificationManager.