print Network Info - Android Network

Android examples for Network:Network Operation

Description

print Network Info

Demo Code


import java.lang.reflect.Method;

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

public class Main {
  private static final String TAG = "";

  public static boolean printNetworkInfo(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
      NetworkInfo in = connectivity.getActiveNetworkInfo();
      Log.i(TAG, "getActiveNetworkInfo: " + in);
      NetworkInfo[] info = connectivity.getAllNetworkInfo();
      if (info != null) {
        for (int i = 0; i < info.length; i++) {
          // if (info[i].getType() == ConnectivityManager.TYPE_WIFI) {
          Log.i(TAG, "NetworkInfo[" + i + "]isAvailable : " + info[i].isAvailable());
          Log.i(TAG, "NetworkInfo[" + i + "]isConnected : " + info[i].isConnected());
          Log.i(TAG, "NetworkInfo[" + i + "]isConnectedOrConnecting : " + info[i].isConnectedOrConnecting());
          Log.i(TAG, "NetworkInfo[" + i + "]: " + info[i]);
          // }//  w ww  . j a va  2 s  . c  o  m
        }
        Log.i(TAG, "\n");
      } else {
        Log.i(TAG, "getAllNetworkInfo is null");
      }
    }
    return false;
  }

  public static boolean isAvailable(Context context) {
    return isWifiAvailable(context) || (isMobileAvailable(context) && isMobileEnabled(context));
  }

  public static boolean isConnected(Context context) {
    NetworkInfo net = getConnManager(context).getActiveNetworkInfo();
    return net != null && net.isConnected();
  }

  public static boolean isConnectedOrConnecting(Context context) {
    NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
    if (nets != null) {
      for (NetworkInfo net : nets) {
        if (net.isConnectedOrConnecting()) {
          return true;
        }
      }
    }
    return false;
  }

  public static boolean isWifiAvailable(Context context) {
    NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
    if (nets != null) {
      for (NetworkInfo net : nets) {
        if (net.getType() == ConnectivityManager.TYPE_WIFI) {
          return net.isAvailable();
        }
      }
    }
    return false;
  }

  public static boolean isMobileAvailable(Context context) {
    NetworkInfo[] nets = getConnManager(context).getAllNetworkInfo();
    if (nets != null) {
      for (NetworkInfo net : nets) {
        if (net.getType() == ConnectivityManager.TYPE_MOBILE) {
          return net.isAvailable();
        }
      }
    }
    return false;
  }

  public static boolean isMobileEnabled(Context context) {
    try {
      Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
      getMobileDataEnabledMethod.setAccessible(true);
      return (Boolean) getMobileDataEnabledMethod.invoke(getConnManager(context));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return true;
  }

  public static ConnectivityManager getConnManager(Context context) {
    return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  }
}

Related Tutorials