Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM BroadcastReceiver setResultCode use

I'm using the GCM example from android developers and couldn't understand the purpose of the setResultCode(Activity.Result_OK). which component receiving this message? who is calling it and receiving it?

here is the example

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK); // ?????
}
}

thanks.

like image 299
joseRo Avatar asked Mar 19 '23 03:03

joseRo


2 Answers

The broadcast in which your app receives the GCM message is an ordered broadcast.

Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.

This means that if your app has multiple broadcast receivers configured to handle GCM messages, they will be called one after the other (in an order that depends on their priority). You might have multiple broadcast receivers handling GCM messages if, for example, your app uses some third party library that uses GCM, and in addition your app uses GCM directly. In this case you want your app to handle only its own messages and not the messages sent to the 3rd party library (and vice versa).

If one of them handles the GCM message and you don't want the others to handle it too, you can abort the broadcast. You do it by setResultCode(Activity.RESULT_CANCEL). On the other hand, setResultCode(Activity.RESULT_OK) would propagate the result of the first receiver to the next receiver.

In that case, your code would look like this :

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras ().get("from").equals (SENDER_ID_OF_YOUR_APP) {
          ComponentName comp = new ComponentName(
            GcmIntentService.class.getPackage().getName(),
            GcmIntentService.class.getName());
          startWakefulService(context, (intent.setComponent(comp)));
          // abort the broadcast
          setResultCode(Activity.RESULT_CANCEL);
        } else
          // don't abort the broadcast
          setResultCode(Activity.RESULT_OK);
        }
    }
}

If you have just one broadcast receiver handling the GCM message, the call to setResultCode does matter much.

like image 140
Eran Avatar answered Mar 28 '23 20:03

Eran


Actually, setResultCode(Activity.RESULT_CANCEL) on its own will not abort the broadcast. You have to call abortBroadcast() :

http://developer.android.com/reference/android/content/BroadcastReceiver.html#abortBroadcast()

Sets the flag indicating that this receiver should abort the current broadcast; only works with broadcasts sent through Context.sendOrderedBroadcast. This will prevent any other broadcast receivers from receiving the broadcast.

like image 24
Mattias Avatar answered Mar 28 '23 18:03

Mattias