2

Why does this work:

    sendBroadcast(MyUtil.configMyIntent(
                      new Intent(),
                      ActionType.DATE,
                      new Date().toString()));

with:

    public static Intent configMyIntent(Intent intent, ActionType actionType, String content){

        intent.setAction("myCustomBroadcastIntentAction");
        intent.putExtra("actionType",actiontype);
        intent.putExtra("content", content);
        return intent;
    }

But when using a subclass:

CustomIntent intent = new CustomIntent(ActionType.DATE, new Date().toString());
sendBroadcast(intent);

with

public class CustomIntent extends Intent {

  public CustomIntent(ActionType actionType, String content) {
    super();
    this.setAction("myCustomBroadcastIntentAction");
    this.putExtra("actionType",actionType);
    this.putExtra("content", content);
  }
}

the extras are not added to the intent and are null when receiving in the BroadcastReceiver?

A.L.
  • 144
  • 2
  • 9

2 Answers2

1

I have not pinpointed the problem in your code. But, I think maybe the ActionType (which I presume is an enum) is an area to investigate. Does ActionType need to be serializable?

In any case, this code works for me. Maybe it's useful:

public class MainActivity extends AppCompatActivity {

    BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v("MyBroadcastReceiver", "action type:" + intent.getSerializableExtra("actionType") +
                    " myExtra:" + intent.getStringExtra("myExtra"));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("myAction"));

        Intent standardIntent = new Intent("myAction");
        standardIntent.putExtra("actionType", ActionType.DATE);
        standardIntent.putExtra("myExtra", "standard value");
        LocalBroadcastManager.getInstance(this).sendBroadcast(standardIntent);

        Intent customIntent = new CustomIntent(ActionType.DATE, "custom value");
        LocalBroadcastManager.getInstance(this).sendBroadcast(customIntent);
    }

    private static class CustomIntent extends Intent {
        CustomIntent(ActionType actionType, String val) {
            super();
            this.setAction("myAction");
            this.putExtra("actionType", actionType);
            this.putExtra("myExtra", val);
        }
    }

    private enum ActionType implements Serializable {
        DATE
    }

}

This is log output:

V/MyBroadcastReceiver: action type:DATE myExtra:standard value

V/MyBroadcastReceiver: action type:DATE myExtra:custom value

Community
  • 1
  • 1
albert c braun
  • 2,650
  • 1
  • 22
  • 32
  • Hm, interesting. It seems the only difference is that your CustomIntent class is static. Btw, no need to make the enum serializable, enums are serializable. – A.L. Jun 13 '17 at 15:45
  • Ah. Good point about the serializable. My mistake. So that's not the problem. – albert c braun Jun 13 '17 at 15:47
  • If I make my CustomIntent class non-static it still works for me. It also still works if I don't use the LocalBroadcastManager. – albert c braun Jun 13 '17 at 15:55
0

extras are not added to the intent

Your child class must implement Parcelable

Darish
  • 11,032
  • 5
  • 50
  • 70