问题
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");
}
}
回答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());
});
来源:https://stackoverflow.com/questions/33374766/getallnetworkinfo-is-deprecated-in-m-but-its-replacement-has-difference-behavi