Test Internet Connection Android

前端 未结 3 614
失恋的感觉
失恋的感觉 2020-12-06 07:55

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 revis

3条回答
  •  余生分开走
    2020-12-06 08:28

    Try following:

    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

     
    

    else

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

提交回复
热议问题