汤姆, 我有类似的问题,但你不能解决它你和其他人推荐的方式。我的onAvailable回调永远不会打不到互联网连接的WiFi。这是我的代码 @TargetApi(21) public static void connectToWifiNetworkSynchronousApi21(final WifiConfiguration wifiConfig){ NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
builder.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
builder.setNetworkSpecifier(wifiConfig.SSID);
NetworkRequest request = builder.build();
ConnectivityManager cm = (ConnectivityManager)App.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(request,new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// TODO Auto-generated method stub
LOG.debug("onAvailable:{} requested {}", network.toString(), wifiConfig.networkId);
super.onAvailable(network);
}
@Override
public void onLosing(Network network, int maxMsToLive) {
// TODO Auto-generated method stub
super.onLosing(network, maxMsToLive);
}
@Override
public void onLost(Network network) {
// TODO Auto-generated method stub
super.onLost(network);
}
@Override
public void onCapabilitiesChanged(Network network,
NetworkCapabilities networkCapabilities) {
// TODO Auto-generated method stub
super.onCapabilitiesChanged(network, networkCapabilities);
}
@Override
public void onLinkPropertiesChanged(Network network,
LinkProperties linkProperties) {
// TODO Auto-generated method stub
super.onLinkPropertiesChanged(network, linkProperties);
}
});
}