@Test public void subscribe() { IntentFilter intentFilter = new IntentFilter("test_action"); Application application = RuntimeEnvironment.application; TestSubscriber<Intent> o = new TestSubscriber<>(); Subscription subscription = RxBroadcastReceiver.create(application, intentFilter).subscribe(o); o.assertValues(); Intent intent1 = new Intent("test_action").putExtra("foo", "bar"); application.sendBroadcast(intent1); o.assertValues(intent1); Intent intent2 = new Intent("test_action").putExtra("bar", "baz"); application.sendBroadcast(intent2); o.assertValues(intent1, intent2); Intent intent3 = new Intent("test_action_ignored"); application.sendBroadcast(intent3); o.assertValues(intent1, intent2); Intent intent4 = new Intent("test_action").putExtra("bar", "baz"); subscription.unsubscribe(); application.sendBroadcast(intent4); o.assertValues(intent1, intent2); }
@CheckResult @NonNull public static Observable<Intent> create(@NonNull final Context context, @NonNull final IntentFilter intentFilter) { checkNotNull(context, "context == null"); checkNotNull(intentFilter, "intentFilter == null"); return Observable.create(new Observable.OnSubscribe<Intent>() { @Override public void call(final Subscriber<? super Intent> subscriber) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { subscriber.onNext(intent); } }; context.registerReceiver(receiver, intentFilter); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { context.unregisterReceiver(receiver); } })); } }); }
RxBroadcastReceiver { @CheckResult @NonNull public static Observable<Intent> create(@NonNull final Context context, @NonNull final IntentFilter intentFilter) { checkNotNull(context, "context == null"); checkNotNull(intentFilter, "intentFilter == null"); return Observable.create(new Observable.OnSubscribe<Intent>() { @Override public void call(final Subscriber<? super Intent> subscriber) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { subscriber.onNext(intent); } }; context.registerReceiver(receiver, intentFilter); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { context.unregisterReceiver(receiver); } })); } }); } }
RxBroadcastReceiver { @CheckResult @NonNull public static Observable<Intent> create(@NonNull final Context context, @NonNull final IntentFilter intentFilter) { checkNotNull(context, "context == null"); checkNotNull(intentFilter, "intentFilter == null"); return Observable.create(new Observable.OnSubscribe<Intent>() { @Override public void call(final Subscriber<? super Intent> subscriber) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { subscriber.onNext(intent); } }; context.registerReceiver(receiver, intentFilter); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { context.unregisterReceiver(receiver); } })); } }); } private  RxBroadcastReceiver(); }
RxBroadcastReceiver { @CheckResult @NonNull public static Observable<Intent> create(@NonNull final Context context, @NonNull final IntentFilter intentFilter) { checkNotNull(context, "context == null"); checkNotNull(intentFilter, "intentFilter == null"); return Observable.create(new Observable.OnSubscribe<Intent>() { @Override public void call(final Subscriber<? super Intent> subscriber) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { subscriber.onNext(intent); } }; context.registerReceiver(receiver, intentFilter); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { context.unregisterReceiver(receiver); } })); } }); } private  RxBroadcastReceiver(); @CheckResult @NonNull // static Observable<Intent> create(@NonNull final Context context,
      @NonNull final IntentFilter intentFilter); }
RxBroadcastReceiver { @CheckResult @NonNull public static Observable<Intent> create(@NonNull final Context context, @NonNull final IntentFilter intentFilter) { checkNotNull(context, "context == null"); checkNotNull(intentFilter, "intentFilter == null"); return Observable.create(new Observable.OnSubscribe<Intent>() { @Override public void call(final Subscriber<? super Intent> subscriber) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { subscriber.onNext(intent); } }; context.registerReceiver(receiver, intentFilter); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { context.unregisterReceiver(receiver); } })); } }); } private  RxBroadcastReceiver(); @CheckResult @NonNull // static Observable<Intent> create(@NonNull final Context context,
      @NonNull final IntentFilter intentFilter);  }