0

I am trying to understand how to work with the ConnectivityManager, and while I see some good samples Here and Here as well as on the Official documentation.

I already have a BroadcastReceiver setup and listens to the "android.net.conn.CONNECTIVITY_CHANGE" Intent, but I am still confused about how exactly should I figure out the following pseudo logic:

String result = null;
if (WifiConnected)
    result = "Wifi: " + getWifiName();//or ID that can be related to the list of Wifi networks on the device
else if (MobileDataConnected)
{
    String networktype = GetMobileNetworkType();//2G/3G/4G
    String roaming = IsMobileRoaming();//Roam/Home
    result = "Mobile: " + networktype + " " + roaming;
}
else
{
    result = "Not connected";
}

Any help will be greatly appreciated,

Thanks.

Community
  • 1
  • 1
Lik Sif
  • 39
  • 1
  • Someone shared a nice gist that might be of some use to you : https://gist.github.com/emil2k/5130324 – 2Dee Oct 03 '14 at 11:55
  • Thanks!! Together with CommonsWare's answer, I managed to hack what I needed :-) – Lik Sif Oct 03 '14 at 13:35

1 Answers1

1

Call getActiveNetworkInfo() on ConnectivityManager. This returns a NetworkInfo object that will tell you the type (WiFi, mobile data), sub-type, and roaming status.

The only thing I am uncertain of is the 2G/3G/4G stuff. I thought that was the sub-type, but I may be wrong about that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491