4

I have a device to which my app can connect via WIFI, the device however doesn't have internet. I implemented a NetworkCallback to catch changes in connectivity as such:

public class ConnectivityNetworkCallback extends ConnectivityManager.NetworkCallback{

        @Override
        public void onAvailable(android.net.Network network) {
            super.onAvailable(network);

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                mConnectivityManager.bindProcessToNetwork(network);
            else
                ConnectivityManager.setProcessDefaultNetwork(network);

            NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
            int connectionType = networkInfo.getType();
            Log.e("networkCallback", "connectionType: " + networkInfo.getTypeName());
            switch(connectionType){

                case ConnectivityManager.TYPE_WIFI:
                    String networkName = networkInfo.getExtraInfo().substring(1,networkInfo.getExtraInfo().length()-1);

                    Log.e("networkCallback","network type wifi, extra: " + networkName);

                    break;
                case ConnectivityManager.TYPE_MOBILE:
                    Log.e("networkCallback","network type mobile, extra:  " + networkInfo.getExtraInfo());
                    break;
            }
        }

        @Override
        public void onLost(android.net.Network network) {
            super.onLost(network);

            Log.e("networkCallback", "losing active connection");
        }
}

When both my Wifi and mobile data is on and I switch between different wifi networks with internet access the "onAvailable()" fires and prints to log that I'm connected via wifi. As soon as I try to connect to a wifi network with no internet "onAvailable()" method fires and prints that I'm connected via mobile and also the device shows an exclamation mark next to the wifi icon.

Is there a way I still can catch the case where I'm connected to a wifi network with no internet?

Keselme
  • 3,779
  • 7
  • 36
  • 68

3 Answers3

1

I have been struggling with the same issue. What seems to be working for me now is obtaining the network capabilities first:

connectivityManager.getNetworkCapabilities(network)

And then checking both if the connection has internet and has been validated:

private val NetworkCapabilities.hasInternet
    get() = hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
        else -> true
    }

Unfortunately, the NetworkCapabilities.NET_CAPABILITY_VALIDATED flag was only introduced in API 23, that's why I ignore the flag in that case.

argenkiwi
  • 2,134
  • 1
  • 23
  • 26
0

Create Utils

public class CheckInternet extends AsyncTask<Void,Void,Boolean> {

private Consumer mConsumer;
public  interface Consumer { void accept(Boolean internet); }

public  CheckInternet(Consumer consumer) { mConsumer = consumer; execute(); }

@Override protected Boolean doInBackground(Void... voids) { try {
    Socket sock = new Socket();
    sock.connect(new InetSocketAddress("8.8.8.8", 53), 1500);
    sock.close();
    return true;
} catch (IOException e) { return false; } }

@Override protected void onPostExecute(Boolean internet) { mConsumer.accept(internet); }

}

Use this method to check Internet connection

 new InternetCheck(internet -> {
    if (internet){
        check your condition
    }else {
        //error message
    }
Vinay
  • 732
  • 5
  • 8
0

You have to register for network call back using Connectivity Manager.

ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkRequestBuilder.Builder builder = new NetworkRequest.Builder()
//Use builder to set custom network parameter
connMgr.requestNetwork(builder.build(),new ConnectivityNetworkCallback())
Ramesh Yankati
  • 1,197
  • 9
  • 13