Android设备的网络接口名称
也许我问错了问题,但我似乎找不到我要找的东西,所以我会试着在这里问,而不是谷歌 基本上,我有以下代码,如果我使用wifi、3G或其他方式(我想到的是栓系),我可以从中收集信息Android设备的网络接口名称,android,Android,也许我问错了问题,但我似乎找不到我要找的东西,所以我会试着在这里问,而不是谷歌 基本上,我有以下代码,如果我使用wifi、3G或其他方式(我想到的是栓系),我可以从中收集信息 //迭代所有网络接口。 对于(枚举en= NetworkInterface.getNetworkInterfaces();en.hasMoreElements();) { NetworkInterface intf=en.nextElement(); //迭代每个网络接口中的所有IP地址。 对于(枚举枚举enumIPAdd
//迭代所有网络接口。
对于(枚举en=
NetworkInterface.getNetworkInterfaces();en.hasMoreElements();)
{
NetworkInterface intf=en.nextElement();
//迭代每个网络接口中的所有IP地址。
对于(枚举枚举enumIPAddr=
intf.getInetAddresses();enumIPAddr.hasMoreElements();)
{
InetAddress InetAddress=enumIPAddr.nextElement();
//环回地址(127.0.0.1)不算作正在使用的地址
//IP地址。
如果(!iNetAddress.isLoopbackAddress())
{
sLocalIP=iNetAddress.getHostAddress().toString();
sInterfaceName=intf.getName();
}
}
}
我认为这里的重要部分是sInterfaceName=intf.getName()代码>
现在在Galaxy S和Galaxy S选项卡上,当您连接到WiFi时,它似乎返回“eth0”,而当连接到3G时,它似乎返回“pdp0”
也就是说,我只能用1 x Galaxy S和1 x Galaxy S Tab进行测试,因为它们是我仅有的Android设备。我想象网络接口名是由设备管理器在内核的某个地方设置的。我可能会浏览每台设备的内核,但我想一定有人已经找到了这些信息,有没有关于在谷歌上查找或搜索内容的建议?你应该使用更简单的方法。调用ConnectionManager的函数,该函数返回当前活动网络-。拥有NetworkInfo
的实例,您可以调用getType()
和getSubtype()
来获取当前正在使用的网络类型
以下是一个例子:
NetworkInfo info = m_connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_WIMAX)
{
//no restrictions, do some networking
}
else if (netType == ConnectivityManager.TYPE_MOBILE &&
netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
//3G connection
if(!m_telephonyManager.isNetworkRoaming())
{
//do some networking
}
}
有用,但不能回答问题。他想要的是接口的名称,而不是当前的网络类型。
NetworkInfo info = m_connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_WIMAX)
{
//no restrictions, do some networking
}
else if (netType == ConnectivityManager.TYPE_MOBILE &&
netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
//3G connection
if(!m_telephonyManager.isNetworkRoaming())
{
//do some networking
}
}