title: Android RxJava Interval with IntentService
body: <p>I am quite new to rxjava so I wanted to use it with android IntentService and I need to be notified every second during a certain period (similar to Android CountDownTimer. I have decided to try it with rxjava and I have this class:</p>
  
  <pre><code>public class WorkoutService extends IntentService {
  public static final String BUNDLE_EXTRA_MESSENGER = "messenger";
  public static final String BUNDLE_EXTRA_NUMBER_ROUNDS = "nr_rounds";
  public static final String BUNDLE_EXTRA_WORKOUT_DURATION = "workout_duration";
  public static final String BUNDLE_EXTRA_PAUSE_DURATION = "pause_duration";
  private static final int NOTIFICATION_ID = 1;
  public static final int UPDATE_PROGRESS = 2;
  
  /**
   * Target we publish for clients to send messages to IncomingHandler.
   * This is the messenger from the activity
   */
  Messenger messenger;
  
  private NotificationManager notifyManager;
  private NotificationCompat.Builder builder;
  private volatile int maxProgress;
  private int numberOfRounds = 4;
  private int workoutDuration = 7 * 60; //7 minutes
  private int pauseDuration = 90; //1.5 minutes
  private int currentProgress;
  
  public WorkoutService() {
      super("WorkoutService");
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
      Bundle extras = intent.getExtras();
      if (extras != null) {
          messenger = (Messenger) extras.get(BUNDLE_EXTRA_MESSENGER);
          numberOfRounds = extras.getInt(BUNDLE_EXTRA_NUMBER_ROUNDS, numberOfRounds);
          workoutDuration = extras.getInt(BUNDLE_EXTRA_WORKOUT_DURATION, workoutDuration);
          pauseDuration = extras.getInt(BUNDLE_EXTRA_PAUSE_DURATION, pauseDuration);
      }
      maxProgress = numberOfRounds * workoutDuration + ((numberOfRounds - 1) * pauseDuration);
      maxProgress = 10; //TODO: for testing
      showNotification(maxProgress);
      Timber.d("maxProgress %d", maxProgress);
      startWorkout();
  
  }
  
  private void startWorkout() {
      final Observable&lt;Long&gt; observable = Observable
              .interval(1, TimeUnit.SECONDS);
      observable
              .subscribeOn(Schedulers.io())
              .subscribe(new Subscriber&lt;Long&gt;() {
                  @Override
                  public void onCompleted() {
                      Timber.d("onCompleted");
                      unsubscribe();
                      stopForeground(true);
                      stopSelf();
  
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Timber.e("onError");
                  }
  
                  @Override
                  public void onNext(Long aLong) {
                      Timber.d("onNext : " + aLong + "S");
                      updateProgress();
                      if (aLong == maxProgress) {
                          onCompleted();
                      }
                  }
              });
  }
  
  private void showNotification(int maxProgress) {
      Intent notificationIntent = new Intent(this, WorkoutService.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notifyManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      builder = new NotificationCompat.Builder(this);
      builder.setContentTitle(getString(R
              .string.notification_title))
              .setContentIntent(pendingIntent)
              .setSmallIcon(R.mipmap.ic_launcher);
      startForeground(NOTIFICATION_ID, builder.build());
      currentProgress = 0;
      builder.setProgress(maxProgress, currentProgress, false);
      notifyManager.notify(NOTIFICATION_ID, builder.build());
  }
  
  private void sendMessageToActivity(Message message) {
      try {
          if (messenger != null) {
              messenger.send(message);
          }
      } catch (RemoteException e) {
          Timber.e(e, "Error sending message to activity");
      }
  }
  
  private void updateProgress() {
      currentProgress++;
      builder.setProgress(maxProgress, currentProgress, false);
      notifyManager.notify(NOTIFICATION_ID, builder.build());
      Message message = Message.obtain(null, UPDATE_PROGRESS, currentProgress, 0);
      sendMessageToActivity(message);
  }
  
  }
  </code></pre>
  
  <p>The problem is the notification does not get dismissed while it should be and it seems the service does not get stopped even if I explicitly call stopSelf(). In the android documentation it says that this service stops himself when there is no longer work to be done but since I am calling onCompleted and unsubscribe should it not be the case? How can I make sure the observable stops emitting and the flow is terminated? Thks a lot </p>