title: Rx Observable emitting values periodically
body: <p>I have to poll some RESTful endpoint periodically to refresh my android app's data. I also have to pause and resume it based on connectivity (if the phone is offline, there's no need to even try). My current solution is working, but it uses standard Java's <code>ScheduledExecutorService</code> to perform periodic tasks, but I'd like to stay in Rx paradigm.</p>
  
  <p>Here's my current code, parts of which are skipped for brevity.</p>
  
  <pre><code>userProfileObservable = Observable.create(new Observable.OnSubscribe&lt;UserProfile&gt;() {
      @Override
      public void call(final Subscriber&lt;? super UserProfile&gt; subscriber) {
          final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
          final Runnable runnable = new Runnable() {
              @Override
              public void run() {
                  // making http request here
              }
          };
          final List&lt;ScheduledFuture&lt;?&gt;&gt; futures = new ArrayList&lt;ScheduledFuture&lt;?&gt;&gt;(1);
          networkStatusObservable.subscribe(new Action1&lt;Boolean&gt;() {
              @Override
              public void call(Boolean networkAvailable) {
                  if (!networkAvailable) {
                      pause();
                  } else {
                      pause();                        
                      futures.add(scheduledExecutorService.scheduleWithFixedDelay(runnable, 0, SECOND_IN_MILLIS * SECONDS_TO_EXPIRE, TimeUnit.MILLISECONDS));
                  }
              }
  
              private void pause() {
                  for (ScheduledFuture&lt;?&gt; future : futures) {
                      future.cancel(true);
                  }
                  futures.clear();
              }
          });
  
          final Subscription subscription = new Subscription() {
              private boolean isUnsubscribed = false;
  
              @Override
              public void unsubscribe() {
                  scheduledExecutorService.shutdownNow();
                  isUnsubscribed = true;
              }
  
              @Override
              public boolean isUnsubscribed() {
                  return isUnsubscribed;
              }
          };
          subscriber.add(subscription);
      }
  }).multicast(BehaviorSubject.create()).refCount();
  </code></pre>
  
  <p><code>networkStatusObservable</code> is basically a broadcast receiver wrapped into <code>Observable&lt;Boolean&gt;</code>, indicating that the phone is connected to the network.</p>
  
  <p>As I said, this solution is working, but I want to use Rx approach for periodic polling and emitting new <code>UserProfile</code>s, because there are numerous problems with scheduling things manually, which I want to avoid. I know about <code>Observable.timer</code> and <code>Observable.interval</code>, but can't figure out how to apply them to this task (and I'm not sure if I need to use those at all).</p>