3

I have had a good search around and experimented with various answers I'v seen for this but none of them appear to work in my case. I'm looking for a robust solution, so, with out further ado;

I have an unusual case where my app needs to connect to a wifi network that has no internet connectivity to perform some tasks (local network), once this is complete, it needs to drop the connection and reconnect to an alternative network, either mobile, or alternative wifi hotspot that DOES has internet connectivity so that I can do comms.

The problem I have is that my comms queue needs to make sure it actually has a route to the internet when it's about to start doing comms (which are triggered on a connectivity change)

I have tried various calls on ConnectivityManager such as;

isConnected();
isAvailable();

but these return true even when connected to my non-internet capable network.

I need a way of making sure that when connected to my non-internet capable network (or any other non-internet capable network such as a BT Openzone, when not registered and logged in for example) I can be told that there is not route to the internet, and then when I do have a route, I can return a true.

One possibility I considered was a ping, but if possible I'd prefer something a little neater.

I tried requestRouteToHost() but that doesn't do what I expected it to and in fact attempts to change connectivity, which is not what I want.

Thanks

Hamid
  • 4,410
  • 10
  • 43
  • 72

2 Answers2

1

Here is the method I use:

public static boolean isNetworkAvailable(final Context context)  {
    return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}

Here is how to use the method:

if (isNetworkAvailable(context)) {
    // code here
} else {
    // code
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • Does this work for an active connection with no connectivity? My question is quite old now, but I'd like to accept an answer. I found at the time there was no real solution (API wise) that didn't require pinging a remote host or similar. – Hamid May 17 '13 at 10:06
  • This detects whether or not the user has access to the internet or not. I use it in all my applications. – Jared Burrows May 17 '13 at 22:32
  • This would not work for wifi without internet. You should double check that value if first query would fail (e.g. ping 8.8.8.8) – Viktor Yakunin Apr 04 '15 at 15:00
0

https://gist.github.com/allco/d50fc10f523293d22fc3d85efe4aaff2

That's my Rx version of the ConnectivityReporter. It uses different implementations for API level >=24 and <23. It checks only connection to any network. If for example there is wifi without internet it will report true but it's ok since for the most of the users it is enough otherwise the check could be extremely complex since term online has quite broad meaning.

Usage:

ConnectivityReporter(context)
        .statesStream()
        .subscribe(
            { connected -> Timber.d("connectivity state: $connected") },
            { err -> Timber.d(err, "error") }
        )
Alexander Skvortsov
  • 2,696
  • 2
  • 17
  • 31