1

I'm using this code to check if an Android device has telephony network connectivity or not.

 final TelephonyManager tel = (TelephonyManager) acontext.getSystemService(Context.TELEPHONY_SERVICE);


if (Build.VERSION.SDK_INT<=24)
        {

            return (tel.getNetworkOperator() != null && !tel.getNetworkOperator().equals(""));
        }
        else
        {
            ConnectivityManager cm = (ConnectivityManager) acontext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            return !(info==null || !info.isConnected());
        }

I thought the code was working properly, but maybe I was testing wrong and considering some things as wrong.

1) I thought that both getNetworkOperator() and getActiveNetworkInfo() made a live query to the telephony network to get their info, so if they couldn't return anything it's because there wasn't network connectivity. Documentation about that gave me that impression, although it's true that isn't clearly specified.

2) Due to that I was checking just with setting Airplane mode, which as expected returned null for these functions.

3) But now I've checked that setting in emulator None as signal strength, surprisingly returns that there's connectivity, which I think shouldn't happen, but well, maybe even if the signal is none there's still network connectivity somehow, although I find that strange. Or maybe is it reading a cached value in device memory?

Would this code be enough to check if there's network connectivity or not?

PD: As an extra note, I just want connectivity enough so the user is synchronized to the time of the network (which I guess requires very little... but more than strictly none).

user2638180
  • 1,013
  • 16
  • 37

0 Answers0