how can I check whether device can access Internet via active WiFi Connection?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-04 04:29:28

问题


I am referring to a situation when device is connected to access point but for some reason is blocked from accessing internet using this AP.


回答1:


for check wifi is enabled or not

 WifiManager wfManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(wfManager.isWifiEnabled())
{
    //Code
}
else  
{
 //Code
 }

for check internet connection..

public boolean isOnline() 
{
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) 
    {
            //Try This 
            int i =netInfo.getType() ; 
            System.out.println("Net Type ="+i);    

            return true;
    }
    return false;

}



回答2:


private boolean connectionAvailable() {
    boolean connected = false;
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        //we are connected to a network
        connected = true;
    }
    return connected;
}



回答3:


These look like something that can solve the problem:

How to do a true Java ping from Windows?

How to check if internet connection is present in java?

Detect internet Connection using Java



来源:https://stackoverflow.com/questions/5840760/how-can-i-check-whether-device-can-access-internet-via-active-wifi-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!