确定Android上的连接类型和质量

时间:2014-10-03 11:30:38

标签: android broadcastreceiver android-wifi connectivity

我正在尝试了解如何使用ConnectivityManager,虽然我看到了一些好的示例HereHere以及Official documentation

我已经有一个BroadcastReceiver设置并且监听&​​#34; android.net.conn.CONNECTIVITY_CHANGE"意图,但我仍然感到困惑的是我应该如何确定以下伪逻辑:

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";
}

非常感谢任何帮助,

感谢。

1 个答案:

答案 0 :(得分:1)

Call getActiveNetworkInfo() on ConnectivityManager。这会返回一个NetworkInfo对象,该对象会告诉您type(WiFi,移动数据),sub-typeroaming status

我唯一不确定的是2G / 3G / 4G的东西。我认为是子类型,但我可能错了。