Android检查网络是否经过身份验证

时间:2016-09-01 12:10:38

标签: android authentication connectivity

我需要检查用户是否已连接CONNECTIVITY_SERVICE,因此下面的代码会给我返回isSourceAvailable" true"的结果。

ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        isSourceAvailable = (activeNetworkInfo != null && activeNetworkInfo.isConnected());

但可能他未经过身份验证,所以我尝试使用以下代码,但它返回了我"已完成"相反,它没有经过验证。

SupplicantState supplicantState;
            WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            supplicantState = wifiInfo.getSupplicantState();

enter image description here

1 个答案:

答案 0 :(得分:2)

你必须添加这个

 ConnectivityManager CManager =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo != null && NInfo.isConnectedOrConnecting()) {
    if (InetAddress.getByName("www.xy.com").isReachable(timeout))
    {  
     // host reachable  true
    }
     else
     {    
     // host not reachable  false
     }  
}
return;