content: package com.yl.rxlifecycledemo;
  
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.util.Log;
  
  import com.trello.rxlifecycle2.android.ActivityEvent;
  import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
  
  import java.util.concurrent.TimeUnit;
  
  import butterknife.ButterKnife;
  import io.reactivex.Observable;
  import io.reactivex.Observer;
  import io.reactivex.android.schedulers.AndroidSchedulers;
  import io.reactivex.annotations.NonNull;
  import io.reactivex.disposables.Disposable;
  import io.reactivex.schedulers.Schedulers;
  
  /**
   * compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.1.0'
   * Created by yangle on 2017/7/4.
   */
  
  public class RxLifecycleComponentsActivity extends RxAppCompatActivity {
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_rxlifecycle);
          ButterKnife.bind(this);
  
          initData();
      }
  
      private void initData() {
          // 每隔1s执行一次事件
          Observable.interval(1, TimeUnit.SECONDS)
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
                  .subscribe(new Observer<Long>() {
                      @Override
                      public void onSubscribe(@NonNull Disposable d) {
  
                      }
  
                      @Override
                      public void onNext(@NonNull Long aLong) {
                          Log.i("接收数据", String.valueOf(aLong));
                      }
  
                      @Override
                      public void onError(@NonNull Throwable e) {
  
                      }
  
                      @Override
                      public void onComplete() {
  
                      }
                  });
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          Observable.interval(1, TimeUnit.SECONDS)
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .compose(this.<Long>bindToLifecycle())
                  .subscribe(new Observer<Long>() {
                      @Override
                      public void onSubscribe(@NonNull Disposable d) {
  
                      }
  
                      @Override
                      public void onNext(@NonNull Long aLong) {
                          Log.i("接收数据", String.valueOf(aLong));
                      }
  
                      @Override
                      public void onError(@NonNull Throwable e) {
  
                      }
  
                      @Override
                      public void onComplete() {
  
                      }
                  });
      }
  }
  