我正在尝试了解如何使用ConnectivityManager,虽然我看到了一些好的示例Here和Here以及Official documentation。
我已经有一个BroadcastReceiver设置并且监听" 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";
}
非常感谢任何帮助,
感谢。
答案 0 :(得分:1)
Call getActiveNetworkInfo()
on ConnectivityManager
。这会返回一个NetworkInfo
对象,该对象会告诉您type(WiFi,移动数据),sub-type和roaming status。
我唯一不确定的是2G / 3G / 4G的东西。我认为是子类型,但我可能错了。