How to observe network changes in RxAndroid

I'm using the code given here .

I put these blocks of code as classes in my projects using the package. And then in the main activity class I wrote this.

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");
        }
    }));

}

      

My goal is to watch for changes and change the MyApp.isConnected variable defined in the MyApp class statically when the network changes to true false. Help would be appreciated. Thanks 😄

+3


source to share


2 answers


Try using 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();
    }
}

      

+4


source


You asked me to answer in another thread. I answer late because I need some time to develop and test a solution that I think is good enough.

I recently created a new project called ReactiveNetwork .

It's open source and available at: https://github.com/pwittchen/ReactiveNetwork .

You can add the following dependency to your file build.gradle

:

dependencies {
  compile 'com.github.pwittchen:reactivenetwork:x.y.z'
}

      

Then you can replace x.y.z

with the latest version number.



After that, you can use the library like this:

 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");
        }
      }
    });

      

You can also use a method filter(...)

from RxJava if you only want to respond to one type of event.

You can create a subscription in a method onResume()

and then cancel it in a method onPause()

inside an Activity.

On the project's GitHub site, you can find more use cases and a sample application.

Also, you can read about NetworkInfo.State enum from Android API, which is now used by the library.

+6


source







All Articles