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

后端 未结 30 3346
猫巷女王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:53

    You can use this method to detect network availability-

    public static boolean isDeviceOnline(Context context) {
            boolean isConnectionAvail = false;
            try {
                ConnectivityManager cm = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                return netInfo.isConnected();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return isConnectionAvail;
        }
    

提交回复
热议问题