Which one is a better way to check internet connection in android in given below functions?

StackOverflow https://stackoverflow.com/questions/22162326

  •  19-10-2022
  •  | 
  •  

Frage

If we are using any type of internet connection than It is better to check internet connectivity status before making any HTTP Requests to avoid http exceptions. we can do this in given way

1- We can use the below function/method

public static boolean getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        boolean status = false;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = true;
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = true;
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = false;
        }
        return status;
    }

where NetworkUtil is as follows

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {

    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;


    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        } 
        return TYPE_NOT_CONNECTED;
    }
}

V/S

2- We can use the below function/method

public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null) 
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) 
                  for (int i = 0; i < info.length; i++) 
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }

QUESTION==> My question is which one is a better practice to use the function/method as in the first function/method we are only interested in mobile or wifi status however in second function/method we are checking for available connection information!

War es hilfreich?

Lösung

I assume you refer NetworkUtil in this github, it is not part of Android SDK; ConnectivityManager is (Added in API Level 1, i.e. from the very beginning of Android SDK).

Always use Android SDK functions instead of 3rd party libraries if possible, as there will be better support, and compatible with future Android OS.

In short, use ConnectivityManager until it is deprecated.

Reference: ConnectivityManager

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top