Skip to content

Usage for BroadcastReceiver

MarcinMoskala edited this page Feb 10, 2017 · 4 revisions

Usage for BroadcastReceiver is pretty the same as [Activities](Usage for Activities) with following differences:

  • In generated class, there is no method for BroadcastReceiver start. Only getIntent, which is standard way to provide BroadcastReceiver intent.
  • On BroadcastReceiver we can use only non-project types. So Parcelable and Serializable are prohibited.
  • Arguments also need to be filled, but there is no onCreate method. It should be done on onReceive. Also there needs to be provided both BroadcastReceiver instance and intent, like in following code:
public class NotificationPublisher extends BroadcastReceiver {

    @Arg int id;

    public NotificationPublisher() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        ActivityStarter.fill(this, intent);
        //... Example: Log.i("SomeService", "Id: " + id);
    }
}

It also can be done in BaseBroadcastReceiver.