Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何暂停/恢复可观察到的事件?_Android_Rx Java - Fatal编程技术网

Android 如何暂停/恢复可观察到的事件?

Android 如何暂停/恢复可观察到的事件?,android,rx-java,Android,Rx Java,我试图通过改造实现分页,但我很难找到如何暂停一个可观察到的页面,这样它就不会继续请求不需要的页面 基本问题是:我能告诉一个可观察的源“暂停”和“恢复”吗?我不是说缓冲或跳过,而是希望源observable完全停止,即:不要发出任何web请求,等等 下面是一些我正在使用的模拟代码。rangeObservable是模拟的Web服务器“寻呼机”,timerObservable就像接收滚动事件一样 package example.wanna.be.pausable; import java.io.IO

我试图通过改造实现分页,但我很难找到如何暂停一个可观察到的页面,这样它就不会继续请求不需要的页面

基本问题是:我能告诉一个可观察的源“暂停”和“恢复”吗?我不是说缓冲或跳过,而是希望源observable完全停止,即:不要发出任何web请求,等等

下面是一些我正在使用的模拟代码。rangeObservable是模拟的Web服务器“寻呼机”,timerObservable就像接收滚动事件一样

package example.wanna.be.pausable;

import java.io.IOException;
import java.lang.Throwable;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.observables.ConnectableObservable;
import rx.Subscription;
import rx.Subscriber;

public class Main {

  private static ConnectableObservable rangeObservable;

  private static void setPaused(boolean paused) {
    // How do I pause/resume rangeObservable?
  }

  public static void main(String[] args) {

    rangeObservable = Observable.range(0, Integer.MAX_VALUE).publish();
    Observable timerObservable = Observable.timer(2, 2, TimeUnit.SECONDS);

    rangeObservable.subscribe(new Subscriber<Integer>() {

      private int count = 0;

      public void onStart() {
        System.out.println("Range started");
      }

      public void onNext(Integer i) {
        System.out.println("Range: " + i);

        if (++count % 20 == 0) {
          System.out.println("Pausing");
          setPaused(true);
        }
      }

      public void onError(Throwable e) {
        e.printStackTrace();
      }

      public void onCompleted() {
        System.out.println("Range done");
      }

    });

    timerObservable.subscribe(new Subscriber<Long>() {

      public void onStart() {
        System.out.println("Time started");

        // I dont know where to put this
        // rangeObservable.connect();
      }

      public void onNext(Long i) {
        System.out.println("Timer: " + i);
        setPaused(false);
      }

      public void onError(Throwable e) {
        e.printStackTrace();
      }

      public void onCompleted() {
        System.out.println("Timer done");
      }

    });

    // for some reason I have to do this or it just exits immediately
    try {
      System.in.read();
    } catch(IOException e) {
      e.printStackTrace();
    }

  }

}
package example.want.be.pausable;
导入java.io.IOException;
导入java.lang.Throwable;
导入java.util.concurrent.TimeUnit;
进口接收。可观察;
导入rx.Observable.ConnectableObservable;
进口接收订阅;
输入接收用户;
公共班机{
私有静态可连接可观察范围可观察;
私有静态void setPaused(布尔暂停){
//如何暂停/恢复可观察的范围?
}
公共静态void main(字符串[]args){
rangeObservable=Observable.range(0,整数.MAX_值).publish();
可观测的timerObservable=可观测的计时器(2,2,TimeUnit.s);
rangeObservable.subscribe(新订户(){
私有整数计数=0;
public void onStart(){
System.out.println(“范围开始”);
}
公共void onNext(整数i){
System.out.println(“范围:+i”);
如果(++计数%20==0){
System.out.println(“暂停”);
设置暂停(真);
}
}
公共无效申报人(可丢弃的e){
e、 printStackTrace();
}
未完成的公共无效(){
System.out.println(“范围完成”);
}
});
timerObservable.subscribe(新订户(){
public void onStart(){
System.out.println(“启动时间”);
//我不知道该把这个放在哪里
//rangeObservable.connect();
}
下一页(长i){
System.out.println(“计时器:+i”);
设置暂停(假);
}
公共无效申报人(可丢弃的e){
e、 printStackTrace();
}
未完成的公共无效(){
System.out.println(“计时器完成”);
}
});
//出于某种原因,我必须这样做,否则它会立即退出
试一试{
System.in.read();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}

感谢您的指导

您需要存储您的订阅并调用unsubscribe/subscribe(尚未对此进行全面测试,但我认为它应该可以工作,我在setPaused中的大部分更改可能需要修复代码重复):


这是因为在main()函数中,订阅是异步启动的,因此您的程序到达main的末尾,然后退出,因为没有更多的代码要运行(因为您的可观察对象在不同的线程上运行)。

如果暂停时收到的值可以删除,您可以使用带有原子布尔门的过滤器来允许传递值,也可以不传递值。我认为方法是让一个可观察对象在您选择新“页面”时发出值。确定它是哪一页,将其用作改装调用的参数。
package example.wanna.be.pausable;

import java.io.IOException;
import java.lang.Throwable;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.observables.ConnectableObservable;
import rx.Subscription;
import rx.Subscriber;

public class Main {

  private static ConnectableObservable rangeObservable;
  Subscription mSubscription;

  private static void setPaused(boolean pause) {
    if (pause) {
        mSubscription.unsubscribe()
    } else {
        mSubscription.subscribe(new Subscriber<Integer>() {

      private int count = 0;

      public void onStart() {
        System.out.println("Range started");
      }

      public void onNext(Integer i) {
        System.out.println("Range: " + i);

        if (++count % 20 == 0) {
          System.out.println("Pausing");
          setPaused(true);
        }
      }

      public void onError(Throwable e) {
        e.printStackTrace();
      }

      public void onCompleted() {
        System.out.println("Range done");
      }

    });
  }

  public static void main(String[] args) {

    rangeObservable = Observable.range(0, Integer.MAX_VALUE).publish();
    Observable timerObservable = Observable.timer(2, 2, TimeUnit.SECONDS);

    mSubscription = rangeObservable.subscribe(new Subscriber<Integer>() {

      private int count = 0;

      public void onStart() {
        System.out.println("Range started");
      }

      public void onNext(Integer i) {
        System.out.println("Range: " + i);

        if (++count % 20 == 0) {
          System.out.println("Pausing");
          setPaused(true);
        }
      }

      public void onError(Throwable e) {
        e.printStackTrace();
      }

      public void onCompleted() {
        System.out.println("Range done");
      }

    });

    timerObservable.subscribe(new Subscriber<Long>() {

      public void onStart() {
        System.out.println("Time started");

        // I dont know where to put this
        // rangeObservable.connect();
      }

      public void onNext(Long i) {
        System.out.println("Timer: " + i);
        setPaused(false);
      }

      public void onError(Throwable e) {
        e.printStackTrace();
      }

      public void onCompleted() {
        System.out.println("Timer done");
      }

    });

    // for some reason I have to do this or it just exits immediately
    try {
      System.in.read();
    } catch(IOException e) {
      e.printStackTrace();
    }

  }

}
// for some reason I have to do this or it just exits immediately
        try {
          System.in.read();
        } catch(IOException e) {
          e.printStackTrace();
        }