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

后端 未结 30 3523
猫巷女王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条回答
  •  猫巷女王i
    2020-11-21 05:13

    It is not complex to check Android network / internet connectivity status. The below DetectConnection class will help you to check this status:

    import android.content.Context;
    import android.net.ConnectivityManager;
    
    public class DetectConnection {
        public static boolean checkInternetConnection(Context context) {
            ConnectivityManager con_manager = (ConnectivityManager) context
                                    .getSystemService(Context.CONNECTIVITY_SERVICE);
    
            if (con_manager.getActiveNetworkInfo() != null
                && con_manager.getActiveNetworkInfo().isAvailable()
                && con_manager.getActiveNetworkInfo().isConnected()) {
                    return true;
            } else {
                return false;
            }
        }
    }
    

    For more details visit How to Check Android Network / Internet Connectivity Status

提交回复
热议问题