How to check internet access on Android? InetAddress never times out

后端 未结 30 3370
猫巷女王i
猫巷女王i 2020-11-21 04:45

I got a AsyncTask that is supposed to check the network access to a host name. But the doInBackground() is never timed out. Anyone have a clue?

30条回答
  •  清歌不尽
    2020-11-21 04:46

    Best approach:

    public static boolean isOnline() {
        try {
        InetAddress.getByName("google.com").isReachable(3);
    
        return true;
        } catch (UnknownHostException e){
        return false;
        } catch (IOException e){
        return false;
        }
        }
    

提交回复
热议问题