Having internet access is more than a simple condition, if you think about it it's more like a stream of booleans, sometimes it's true, sometimes it's false.
What you want is to create an Observable that fires true when an internet connection becomes available.
If you're on Android, you can have a BehaviourSubject in a broadcast receiver
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean hasInternet = wifi.isAvailable() || mobile.isAvailable()
subject.onNext(hasInternet);
}
}
You still need to somehow pass the subject to your broadcast receiver but it shouldn't be a big deal.
Then, in order to subscribe to your observable only when this subject returns true, you can do it like so:
subject
.filter(hasInternet ->
hasInternet // Don't continue if hasInternet is false
)
.flatMap(o ->
yourObservable // At this point, return the observable cause we have internet
)
.subscribe() // subscribe