测试因特网连接的Andr​​oid因特网、测试、oid、Andr

2023-09-04 07:49:19 作者:殃国妲己

希望我可以做一个测试,以验证网络连接,我不想检查网络状态,因为它只有当我已经激活了上网设备上,Y哟quiero revisar SI ES更多钞票conectarse一个网络检测。有点像平。

Wish I could do a test to verify internet connection, I don't want check network state, because it only detects if I have activated internet on my device, y yo quiero revisar si es posible conectarse a internet. Something like a ping.

推荐答案

请尝试以下操作:

public boolean checkOnlineState() {
    ConnectivityManager CManager =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo NInfo = CManager.getActiveNetworkInfo();
    if (NInfo != null && NInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

不要忘了访问

dont forget the access

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

其他

else

if (InetAddress.getByName("www.xy.com").isReachable(timeout))
{    }
else
{    }