我想使用连接管理器,它提供方法getAllNetworkInfo()来检查Android中的网络可用性.此方法在API级别23中已弃用.而Developer doc建议使用getAllNetworks().我试过但是没有得到我从旧代码中获得的确切功能.请有人指导我如何使用getAllNetworks()方法.以下是我正在使用的代码:
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
@SuppressWarnings("deprecation")
NetworkInfo[] info = connectivity.getAllNetworkInfo();
//use getAllNetworks() instead
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)