我需要通过wifi将手机连接到连接的设备。这个热点没有互联网。
在5.0之前我确实喜欢这个以检查我是否已连接到wifi
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mWifi.isConnected()&& wifiManager.getConnectionInfo().getNetworkId()==addedNetwork)
但是现在有了5.0版本的android,当我检查isConnected时,它总是返回false,因为没有连接到互联网。
我该如何处理这个案子?
此致
答案 0 :(得分:0)
我没有试过这个,但根据谷歌docs这应该有效
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new Builder();
//Indicates that this network should be able to reach the internet.
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
NetworkRequest build = builder.build();
cManager.requestNetwork(build, new NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
// do what you want to do here
}
});`