android - Android 设备的网络接口(interface)名称

标签 android

也许我问错了问题,但我似乎找不到我要找的东西,所以我会试着在这里问而不是谷歌。

基本上,我有以下代码,如果我使用的是 wifi、3G 或其他东西(想到网络共享),我可以从中收集信息。

    // Iterate over all network interfaces.
    for (Enumeration<NetworkInterface> en = 
        NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
        {
            NetworkInterface intf = en.nextElement();
            // Iterate over all IP addresses in each network interface.
            for (Enumeration<InetAddress> enumIPAddr = 
                intf.getInetAddresses(); enumIPAddr.hasMoreElements();)
            {
                InetAddress iNetAddress = enumIPAddr.nextElement();
                // Loop back address (127.0.0.1) doesn't count as an in-use 
                // IP address.
                if (!iNetAddress.isLoopbackAddress())
                {
                    sLocalIP = iNetAddress.getHostAddress().toString();
                    sInterfaceName = intf.getName();
                }
            }
        }

我认为这里的重要部分是 sInterfaceName = intf.getName();

现在在 Galaxy S 和 Galaxy S Tab 上,这似乎在您连接到 WiFi 时返回“eth0”,在连接到 3G 时似乎返回“pdp0”

也就是说,我只能使用 1 x Galaxy S 和 1 x Galaxy S Tab 进行测试,因为它们是我仅有的 Android 设备。我想网络接口(interface)名称是由设备管理器在内核中的某处设置的。我可能可以浏览每个设备的内核,但我想一定有人已经找到了这些信息,关于在何处查看或在 google 中搜索什么有什么建议吗?

最佳答案

您应该使用更简单的方法。调用 ConnectivityManager 的函数返回当前 Activity 网络 - getActiveNetworkInfo() .有了 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
   }      
}

关于android - Android 设备的网络接口(interface)名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010903/

相关文章:

Android、HttpURLConnection 和处理错误的凭据

android - 是否可以使用 Flutter 阅读谷歌地图导航通知?

android - - 在 android studio 2.0 中使用新的即时运行功能后应用需要 30 秒才能启动?

Android Web View 无法从亚马逊服务网址查看 PDF

android - 加载资源失败:net::ERR_CLEARTEXT_NOT_PERMITTED

android - WebView.capturePicture 因 url 参数而失败

android - 在 Android 上从 GPS 获取速度计算距离和时间

javascript - 单击行时无法转到新 Activity

android - 没有这样的属性 : variantManager

android - Android Studio生成的<module>/release/output.json是什么