如何观察RxAndroid中的网络变化

Sai*_*sin 5 android reactive-programming rx-java rx-android

我正在使用这里给出的代码.

我将这些代码块作为类放在我的项目的util包中.然后在主要活动类中我写了这个..

class MenuActivity {

// Variable declaration
  private final CompositeSubscription mConnectionSubscription = new CompositeSubscription();

@Override
protected void onCreate(Bundle savedInstanceState) {

    // Some initialisation of UI elements done here

    mConnectionSubscription.add(AppObservable.bindActivity(this, NetworkUtils.observe(this)).subscribe(new Action1<NetworkUtils.State>() {
        @Override
        public void call(NetworkUtils.State state) {
            if(state == NetworkUtils.State.NOT_CONNECTED)
                Timber.i("Connection lost");
            else
                Timber.i("Connected");
        }
    }));

}
Run Code Online (Sandbox Code Playgroud)

我的目标是监视更改并在网络更改为true false时静态更改MyApp类中定义的变量MyApp.isConnected.帮助将不胜感激.谢谢

pio*_*hen 7

你在另一个帖子里问我答案.我回答得很晚,因为我需要一些时间来开发和测试解决方案,我觉得这很好.

我最近创建了一个名为ReactiveNetwork的新项目.

它是开源的,可从以下网址获得:https://github.com/pwittchen/ReactiveNetwork.

您可以将以下依赖项添加到您的build.gradle文件中:

dependencies {
  compile 'com.github.pwittchen:reactivenetwork:x.y.z'
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以x.y.z使用最新版本号替换.

之后,您可以通过以下方式使用库:

 ReactiveNetwork.observeNetworkConnectivity(context)        
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<ConnectivityStatus>() {
      @Override public void call(Connectivity connectivity) {
        if(connectivity.getState() == NetworkInfo.State.DISCONNECTED) {
          Timber.i("Connection lost");
        } else if(connectivity.getState() == NetworkInfo.State.CONNECTED) {
          Timber.i("Connected");
        }
      }
    });
Run Code Online (Sandbox Code Playgroud)

filter(...)如果您只想对单一类型的事件做出反应,也可以使用RxJava中的方法.

您可以在onResume()方法中创建订阅,然后onPause()在Activity 中的方法中取消订阅.

您可以在GitHub上的项目网站上找到更多使用示例和示例应用程序.

此外,您可以从Android API中读取有关NetworkInfo.State枚举的信息,该API现在由库使用.


Dan*_*nko 3

尝试使用rxnetwork-android

public class MainActivity extends AppCompatActivity{

    private Subscription sendStateSubscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Observable<RxNetwork.State> sendStateStream =
                RxNetwork.stream(this);

        sendStateSubscription = AppObservable.bindActivity(
                this, sendStateStream
        ).subscribe(new Action1<RxNetwork.State>() {
            @Override public void call(RxNetwork.State state) {
                if(state == RxNetwork.State.NOT_CONNECTED)
                    Timber.i("Connection lost");
                else
                    Timber.i("Connected");
            }
        });
    }

    @Override protected void onDestroy() {
        sendStateSubscription.unsubscribe();
        sendStateSubscription = null;

        super.onDestroy();
    }
}
Run Code Online (Sandbox Code Playgroud)