我需要检查用户是否已连接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();
答案 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;