即使以太网可用并已连接,以太网的NetworkInfo.isAvailable()也始终为false

时间:2012-06-05 06:38:42

标签: android networking ethernet

以下是代码:

ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conMan.getActiveNetworkInfo();
Log.e("TEST","=================Info================\n"+info.toString());

logcat看起来像

E/TEST    ( 2113): =================Info================
E/TEST    ( 2113): NetworkInfo: type: ETHERNET[], state: CONNECTED/CONNECTED, reason:    (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false
你认为这很奇怪吗?不可用但已连接......

1 个答案:

答案 0 :(得分:0)

尝试使用此代码检查网络连接

 public boolean isNetworkOnline() {
boolean status=false;
try{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getNetworkInfo(0);
    if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
        status= true;
    }else {
        netInfo = cm.getNetworkInfo(1);
        if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
            status= true;
    }
}catch(Exception e){
    e.printStackTrace();  
    return false;
}
return status;

}