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

后端 未结 30 3305
猫巷女王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 05:13

    I have gone through all the answers and i come up with my own answer which first check whether Internet is available and if Internet is available then it check whether it is active or not.

    I have included all necessary method and classes to check for active Internet connection.

    NetworkUtils.class

    public class NetworkUtils {
    
        public static final int STATUS_CONNECTED = 0 ;
    
        public static boolean isInternetAvailable(Context ctx){
            ConnectivityManager cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        }
    
        public static int isInternetActiveWithPing() {
            try {
                Runtime runtime = Runtime.getRuntime();
                Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
                int exitValue = process.waitFor();
                return exitValue;
            } catch (Exception ex) {
                return -1;
            }
        }
    
        public static boolean isInternetActiveWithInetAddress() {
            try {
                InetAddress inetAddress = InetAddress.getByName("www.google.com");
                return inetAddress != null && !inetAddress.toString().equals("");
            } catch (Exception ex) {
                return false;
            }
        }
    
        public static void displayInternetConnectionMessage(Context ctx){
            Toast.makeText(ctx, "Check Internet Connection", Toast.LENGTH_SHORT).show();
        }
    }
    

    You can check whether Internet is active using below code:

     private void checkInternetConnection() {
            if (NetworkUtils.isInternetAvailable(this)) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (NetworkUtils.isInternetActiveWithPing() == NetworkUtils.STATUS_CONNECTED) {
                            performNetworkingOperations();
                        } else {
                            if (NetworkUtils.isInternetActiveWithInetAddress()) {
                                performNetworkingOperations();
                            } else {
                                displayConnectionMessage();
                            }
                        }
                    }
                }).start();
    
            } else {
                displayConnectionMessage();
            }
        }
    
        private void performNetworkingOperations() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "Internet is Available", Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        private void displayConnectionMessage() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    NetworkUtils.displayInternetConnectionMessage(MainActivity.this);
                }
            });
        }
    

提交回复
热议问题