Observe Android's CONNECTIVITY_CHANGE broadcasts using RxJava.

Overview

Rx.Network

Release License

Listen for Android's CONNECTIVITY_CHANGE broadcasts.

Use

Add the ACCESS_NEWORK_STATE permission to AndroidManifest.xml

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Observing

This library exposes three different observables. They all monitor network connectivity the same way, but they provided varying levels of specificity about the state of the network.

Boolean

Use this observable when all you're interested in is knowing whether the network is connected or not.

	ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    RxNetwork.connectivityChanges(this, connectivityManager)
        .subscribe(new Action1<Boolean>()
        {
            @Override
            public void call(Boolean connected)
            {
                // [...]
            }
        }

NetworkInfo.State

If you want just a little more information about the state of the network, but don't want to be weighed down by the minutia of things, then use this observable. Read NetworkInfo.State for more information about what you'll get back.

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    RxNetwork.stateChanges(this, connectivityManager)
        .subscribe(new Action1<NetworkInfo.State>()
        {
            @Override
            public void call(NetworkInfo.State state)
            {
                // [...]
            }
        }

NetworkInfo.DetailedState

Here's the big daddy. This observable will tell you the "fine-grained" state of the network. Read NetworkInfo.DetailedState for more information about what you'll get back.

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    RxNetwork.connectivityChanges(this, connectivityManager)
        .subscribe(new Action1<NetworkInfo.DetailedState>()
        {
            @Override
            public void call(NetworkInfo.DetailedState detailedState)
            {
                // [...]
            }
        }

Binaries

Add the JitPack repository to your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
   }

And then add this library to your project:

   dependencies {
        compile 'io.andref:Rx.Network:1.0.1'
   }

License

Copyright 2016 Michael De Soto

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

You might also like...
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Android library to observe scroll events on scrollable views.
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

Android library to observe scroll events on scrollable views.
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

SharedPreference Library to save all types including your custom types and observe them if need be.

A SharedPreference Library that can be used to store all types including your custom classes and observe them too if needed.

Extension functions over Android's callback-based APIs which allows writing them in a sequential way within coroutines or observe multiple callbacks through kotlin flow.

callback-ktx A lightweight Android library that wraps Android's callback-based APIs into suspending extension functions which allow writing them in a

ViewModel-Lifecycle - ViewModel Lifecycle allows you to track and observe Jetpack ViewModel's lifecycle changes
ViewModel-Lifecycle - ViewModel Lifecycle allows you to track and observe Jetpack ViewModel's lifecycle changes

ViewModel Lifecycle 🌳 ViewModel Lifecycle allows you to track and observe Jetpa

Viewmodel-lifecycle - ViewModel Lifecycle allows you to track and observe Jetpack ViewModel's lifecycle changes
Viewmodel-lifecycle - ViewModel Lifecycle allows you to track and observe Jetpack ViewModel's lifecycle changes

ViewModel Lifecycle 🌳 ViewModel Lifecycle allows you to track and observe Jetpa

🚀🌆🏙 Display differences or animate progress between 2 images or Composables with overlay and customization options, zoom, pan gestures, and progress to observe properties for animating before-after progress

Compose Before-After Composables to display Images, or Composables as before and after composables to display differences or animate progress between

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

RxJava: Reactive Extensions for the JVM RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-base

DrawRoute wraps Google Directions API (https://developers.google.com/maps/documentation/directions/) using RxJava for Android so developers can download, parse and draw path on the map in very fast and flexible way (For now only JSON support).
Showcase project of Functional Reactive Programming on Android, using RxJava.

FunctionalAndroidReference FunctionalAndroidReference is a showcase project of Functional Reactive Programming on Android, using RxJava. It's a compan

🎥 Android App using Kotlin, MVVM, ViewModel and LiveData, RxJava, Retrofit, REST API, OkHttp, Gson, Glide, Paging library and Material Design. In the app you can see a list of popular movies and additional info about every movie.
🎥 Android App using Kotlin, MVVM, ViewModel and LiveData, RxJava, Retrofit, REST API, OkHttp, Gson, Glide, Paging library and Material Design. In the app you can see a list of popular movies and additional info about every movie.

Movies 🎥 Android App using Kotlin, MVVM, ViewModel and LiveData, RxJava, Retrofit, REST API (https://www.themoviedb.org), OkHttp, Gson, Glide, Paging

Instagram clone App in android using Kotlin, LiveData, MVVM, Dagger, RxJava and Retrofit.
Instagram clone App in android using Kotlin, LiveData, MVVM, Dagger, RxJava and Retrofit.

Instagram-Project-in-android-with-MVVM-architecture Project from MindOrks Professional Bootcamp with self practice work and added some additional feat

Android MVVM Architecture using Kotlin, RxJava, Koin.
Android MVVM Architecture using Kotlin, RxJava, Koin.

Android-MVVM-RxJava-Digikala_kotlin Digikala sample app developed MVVM architecture design pattern that follow the best practices of Object Oriented D

RxJava bindings for Android

RxAndroid: Reactive Extensions for Android Android specific bindings for RxJava 3. This module adds the minimum classes to RxJava that make writing re

Android Viper template with Kotlin, Dagger 2, Retrofit & RxJava
Android Viper template with Kotlin, Dagger 2, Retrofit & RxJava

Android VIPER Architecture Example This repository contains a detailed sample client-server app that implements VIPER(View-Interactor-Presenter-Entity

Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API
Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API

What is Postman? Postman is a reactive One-tap SMS verification library. This library allows the usage of RxJava with The SMS User Consent API Usage P

Materially inspired widgets and views that expose RxJava bindings.

Rx.Widgets Materially inspired widgets and views. Use ExpandableButtonGroup Add the widget to your view: io.andref.rx.widgets.ExpandableButtonGro

RxJava 2.x extension to provide meaningful Stack Traces
RxJava 2.x extension to provide meaningful Stack Traces

RxJava2Debug A library to make StackTraces involving RxJava2 more meaningful (they will always point to your code!). Rationale If you use RxJava2, you

Comments
  • Error: Activity has leaked IntentReceiver

    Error: Activity has leaked IntentReceiver

    In Activity's onCreate() I call:

            connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            RxNetwork.connectivityChanges(this, connectivityManager)
                    .subscribe({ connected: Boolean -> if (connected) refreshApp() })
    

    when I press back button this error shows up:

    Activity com.mypackage.MainActivity has leaked IntentReceiver io.andref.rx.content.OnSubscribeBroadcastRegister$1@c58a89f that was originally registered here. Are you missing a call to unregisterReceiver()?
                                                                         android.app.IntentReceiverLeaked: Activity com.mypackage.MainActivity has leaked IntentReceiver io.andref.rx.content.OnSubscribeBroadcastRegister$1@c58a89f that was originally registered here. Are you missing a call to unregisterReceiver()?
                                                                             at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1030)
                                                                             at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:817)
                                                                             at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1256)
                                                                             at android.app.ContextImpl.registerReceiver(ContextImpl.java:1236)
                                                                             at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:583)
                                                                             at io.andref.rx.content.OnSubscribeBroadcastRegister.call(OnSubscribeBroadcastRegister.java:65)
                                                                             at io.andref.rx.content.OnSubscribeBroadcastRegister.call(OnSubscribeBroadcastRegister.java:28)
                                                                             at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
                                                                             at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
                                                                             at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
                                                                             at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
                                                                             at rx.Observable.subscribe(Observable.java:8759)
                                                                             at rx.Observable.subscribe(Observable.java:8726)
                                                                             at rx.Observable.subscribe(Observable.java:8549)
                                                                             at com.mypackage.MainActivity.onCreate(MainActivity.kt:52)
    
    

    It looks like, it comes from OnSubscribeBroadcastRegister's method call()

    Thanks

    opened by palicka 1
Releases(1.0.1)
Owner
Andrefio
Andrefio
Starter code for getting and saving data from the Internet using Kotlin.

Doggos Description Doggos is an app that fetches (pun intended) information from a third party API. The API returns data about a random dog that conta

Latifah President 0 Nov 4, 2021
A basic list application designed using MVVM pattern with Retrofit, OkHttp, and Hilt for dependency injection

FRExercise A basic list application designed using MVVM pattern with Retrofit, O

null 0 Dec 22, 2021
Kotlin-echo-client - Echo client using Kotlin with Ktor networking library

Overview This repository contains an echo server implemented with Kotlin and kto

Elliot Barlas 2 Sep 1, 2022
Kotlin-REST-Retrofit - Simple client to consume a REST API with Retrofit using Kotlin

Kotlin REST Retrofit Sencillo cliente para consumir una API REST con Retrofit us

José Luis González Sánchez 5 Nov 4, 2022
A product registration service using the Kotlin language and the Micronaut and Grpc framework

A product registration service using the Kotlin language and the Micronaut and Grpc framework

Mateus Araújo 30 Nov 2, 2022
Ktorfit - a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Jens Klingenberg 637 Dec 25, 2022
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022
Androids EditText that animates the typed text. EditText is extended to create AnimatedEditText and a PinEntryEditText.

AnimatedEditText for Android This repository contains AnimatedEditText and TextDrawable all of which extend the behaviour of EditText and implement fe

Ali Muzaffar 439 Nov 29, 2022
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

Mike Penz 1.8k Nov 10, 2022