GCM BroadcastReceiver setResultCode use

I am using the GCM example from Android developers and cannot understand the purpose of setResultCode (Activity.Result_OK). which component receives this message? who calls and receives it?

here is an 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); // ?????
}
}

thank.

+4
source share
2 answers

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

( Context.sendOrderedBroadcast) . , , . android: priority ; .

, , GCM, ( , ). , GCM, , , , GCM, , , GCM. , , , ( ).

GCM, , , . setResultCode(Activity.RESULT_CANCEL). , setResultCode(Activity.RESULT_OK) .

:

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);
        }
    }
}

, GCM, setResultCode .

+6

, setResultCode(Activity.RESULT_CANCEL) . abortBroadcast():

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

, , ; , Context.sendOrderedBroadcast. .

+1

Source: https://habr.com/ru/post/1547303/


All Articles