4

ConnectivityManager's getAllNetworkInfo() is deprecated on API 23 and the comments for it say to use getAllNetworks() instead. However I am finding these do not have the same behavior.

For example, if the phone has an active cellular network available but wifi is currently turned on, then getAllNetworkInfo() will return both networks (it'll show wifi as connected and cellular as disconnected).

However getAllNetwork() only returns the wifi network in this situation. If wifi is turned off then it will return the cellular network. In other words it appears to be returning the currently active network only (however there is another method for that which is getActiveNetworkInfo()).

In addition to getAllNetworkInfo() only returning one network, getAllNetworks() is also only retiring one network.

With Marshmallow, how can I get the same behavior as getAllNetworkInfo() i.e. get a list of all networks that are available, whether they are disconnected or connected?

Ultimately I want to know if a cellular data connection is available. At the moment, with the new ConnectivityManager API, I can't see any way of doing this.

If the code below is complied with SDK 22 and run on M then it lists two networks, if getAllNetworkInfo() is swapped for getAllNetworks() (and corresponding changes for Network<->NetworkInfo) and compiled with SDK23 and run on the same device, only one network is listed.

 public static synchronized void checkNetworkConnectivity ()
{
        Context context;
        context = CityIdApplication.getHandsetState().getContext();
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

         NetworkInfo [] networks = cm.getAllNetworkInfo();
        //Network[] networks = cm.getAllNetworks();
        if (networks != null) {
            for (Network network : networks) {
                NetworkInfo info = cm.getNetworkInfo(network);
                if (info.isAvailable()) {
                    if (info.isConnected()) {
                        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                                + "], state: " + info.getDetailedState());
                    } else
                        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                                + "], state: " + info.getDetailedState() + "=== isAvailable");
                } else
                    Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName()
                            + "], state: " + info.getDetailedState() + "=== NOT Available");
            }
    }
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • Which exception will throw when you call getAllNetworkInfo? I test getAllNetworkInfo in android M (API 23) emulator is still worked (though compile with deprecated warning). – kzz Oct 27 '15 at 18:41
  • @kzz, I was mistaken about it throwing. Some of the other CM methods now do throw, such as startUsingNetworkFeature() – Gruntcakes Oct 27 '15 at 19:59
  • Try to dig in the sources of android. :) I believe they changed the behavior of API . and there is no a "legal" way to achieve what you want. – ProblemSlover Oct 27 '15 at 20:16
  • Or you may want to post your issue on their developer's forums: https://source.android.com/source/community.html http://developer.android.com/support.html – ProblemSlover Oct 28 '15 at 02:03

1 Answers1

-1

Why don't you use registerNetworkCallback?

NetworkRequest request = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .build();
final ConnectivityManager manager = getSystemService(ConnectivityManager.class);
manager.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback(){
    @Override
    public void onAvailable(Network network) {
        NetworkInfo info = manager.getNetworkInfo(network);
        Log.v(TAG, "== NETWORK type: " + info.getTypeName() + "[" + info.getSubtypeName() + "], state: " + info.getDetailedState());
    });
cuihtlauac
  • 1,808
  • 2
  • 20
  • 39