How to check currently internet connection is available or not in android

前端 未结 18 1292
轮回少年
轮回少年 2020-12-04 15:40

I want to execute my application offline also, so I need to check if currently an internet connection is available or not. Can anybody tell me how to check if internet is av

相关标签:
18条回答
  • 2020-12-04 16:04

    This will show an dialog error box if there is not network connectivity

        ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    
        if (networkInfo != null && networkInfo.isConnected()) {
            // fetch data
        } else {
            new AlertDialog.Builder(this)
                .setTitle("Connection Failure")
                .setMessage("Please Connect to the Internet")
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
        }
    
    0 讨论(0)
  • 2020-12-04 16:05

    You can use two method :

    1 - for check connection :

       private boolean isNetworkConnected() {
            ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            return cm.getActiveNetworkInfo() != null;
        }
    

    2 - for check internet :

      public boolean internetIsConnected() {
            try {
                String command = "ping -c 1 google.com";
                return (Runtime.getRuntime().exec(command).waitFor() == 0);
            } catch (Exception e) {
                return false;
            }
        }
    

    Add permissions to manifest :

     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    0 讨论(0)
  • 2020-12-04 16:06

    Use the method checkConnectivity:

      if (checkConnectivity()){
        //do something 
    
        }
    

    Method to check your connectivity:

    private boolean checkConnectivity() {
            boolean enabled = true;
    
            ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    
            if ((info == null || !info.isConnected() || !info.isAvailable())) {
                                Toast.makeText(getApplicationContext(), "Sin conexión a Internet...", Toast.LENGTH_SHORT).show();
                return false;
            } else {
                return true;
            }
    
            return false;
        }
    
    0 讨论(0)
  • 2020-12-04 16:10
    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }
    

    Google recommends this code block for checking internet connection. Because the device may have not internet connection even if it is connected to WiFi.

    0 讨论(0)
  • 2020-12-04 16:13
      public  boolean isInternetConnection()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
                    connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                //we are connected to a network
                return  true;
            }
            else {
                return false;
            }
        }
    
    0 讨论(0)
  • 2020-12-04 16:14

    This will tell if you're connected to a network:

    boolean connected = false;
    ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
            //we are connected to a network
            connected = true;
        }
        else
            connected = false;
    

    Warning: If you are connected to a WiFi network that doesn't include internet access or requires browser-based authentication, connected will still be true.

    You will need this permission in your manifest:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    0 讨论(0)
提交回复
热议问题