how can I check whether device can access Internet

2019-07-02 00: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.

3条回答
闹够了就滚
3楼-- · 2019-07-02 00:54

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;

}
查看更多
Bombasti
4楼-- · 2019-07-02 00:56
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;
}
查看更多
登录 后发表回答