title: Wrapping event listeners in Observables
body: <p>I've seen a lot of examples of how to turn finite things like arrays or <code>Iterable</code>s into <code>Observable</code>s, but I'm not sure I understand how to make an <code>Observable</code> out of something live and effectively unbounded like an event receiver.  I studied the RxJava2 docs and came up with this, using an Android <code>LocationListener</code> as an example.</p>
  
  <p><strong>Is there a simpler and/or more correct way to do this?</strong>  I'm aware of the "RxBus" concept, but it seems like a way of clinging to the old event bus paradigm.</p>
  
  <pre><code>final Observable&lt;Location&gt; locationObservable = Observable.create(new ObservableOnSubscribe&lt;Location&gt;() {
      final LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  
      @Override
      public void subscribe(final ObservableEmitter&lt;Location&gt; emitter) throws Exception {
          final LocationListener listener = new LocationListener() {
              @Override
              public void onLocationChanged(final Location location) {
                  emitter.onNext(location);
              }
  
              @Override
              public void onStatusChanged(final String s, final int i, final Bundle bundle) {
                  // TODO ???
              }
  
              @Override
              public void onProviderEnabled(final String s) {
                  // TODO ???
              }
  
              @Override
              public void onProviderDisabled(final String s) {
                  // TODO ???
              }
          };
  
          mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
  
          emitter.setCancellable(new Cancellable() {
              @Override
              public void cancel() throws Exception {
                  mLocationManager.removeUpdates(listener);
              }
          });
  
          emitter.setDisposable(new Disposable() {
              private AtomicBoolean mDisposed;
  
              @Override
              public void dispose() {
                  if(mDisposed.compareAndSet(false, true)) {
                      mLocationManager.removeUpdates(listener);
                  }
              }
  
              @Override
              public boolean isDisposed() {
                  return mDisposed.get();
              }
          });
      }
  });
  </code></pre>