1

I have this code:

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

NetworkInfo networkInfoWifi =
  connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

NetworkInfo networkInfoMobileData =
  connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

And then in my Android network settings, both Mobile Data and WiFi are enabled. The following checks are

networkInfoWifi.isConnected();       // results to TRUE
networkInfoMobileData.isConnected(); // results to FALSE

My app runs on Android Lollipop. I expect that both checks above should return TRUE. Why is this happening? I need them to be both TRUE. What should I do?

user1506104
  • 6,554
  • 4
  • 71
  • 89

2 Answers2

0

ConnectivityManager.getNetworkInfo() is a deprecated method.

The right thing to do is to make a ConnectivityManager.NetworkCallback

To answer your question, checking whether you are connected to the internet via WiFi or Mobile Data:

    ConnectivityManager connMgr = (ConnectivityManager) 
    getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest request = new NetworkRequest.Builder()
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .build();

    connMgr.requestNetwork(request, new ConnectivityManager.NetworkCallback() {

        // This callback is invoked when there is a valid Mobile Data or WiFi network 
        // but is later lost during the course of using the application.
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);
        }


        // This callback is invoked when there is a valid Mobile Data or WiFi network 
        // that is ready for use
        @Override
        public void onLost(@NonNull Network network) {
            super.onLost(network);
        }


        // This callback is invoked when there is no valid Mobile Data or WiFi network 
        // after a specific timeout has timed out.
        @Override
        public void onUnavailable() {
            super.onUnavailable();
        }
    });

There are various other callbacks, but I believe for your use case these are the primary ones you would need. Finally, the to add a specific timeout, that was mentioned in the onUnavailable callback, you can use this Constructor for your ConnectivityManager.NetworkCallback:

ConnectivityManager.NetworkCallback(int timeoutMs)

I really do hope this helps. See more information on the API here

  • Hello there. Thank you for your answer. By the way, I am targeting a Lollipop device. `ConnectivityManager.getNetworkInfo()` is still valid on this platform. https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkInfo(android.net.Network) – user1506104 Jun 19 '20 at 10:48
  • @user1506104 okay then. – Favour Felix Chinemerem Jun 19 '20 at 14:03
0

I never found a way to make the networkInfoWifi.isConnected() and networkInfoMobileData.isConnected() to return both TRUE. This is probably because Android can only be connected to only one interface at a time.

This interface selection is also done by Android automatically starting Lollipop devices. Android Lollipop defaults to Mobile Data when Wi-Fi has not Internet access?

user1506104
  • 6,554
  • 4
  • 71
  • 89