Android设备的网络接口名称 [英] Network Interface names for Android devices

查看:95
本文介绍了Android设备的网络接口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我问错了一个问题,但似乎找不到我要寻找的东西,因此我将尝试在这里而不是Google询问它.

Maybe I'm asking the wrong question, but I can't seem to find what I'm looking for, so I'll try ask it here instead of google.

基本上,我有以下代码,如果我使用的是wifi,3G或其他某种设备(可以想到网络共享),就可以从中收集代码.

Basically, I have the following code, from which I can glean if I am on wifi, 3G or something else (tethering comes to mind).

    // 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();

I believe the important part here is sInterfaceName = intf.getName();

现在在Galaxy S和Galaxy S选项卡上,当您连接到WiFi时似乎返回"eth0",而在连接3G时似乎返回"pdp0"

Now on the Galaxy S and Galaxy S Tab, this seems to return "eth0" when you are connected to WiFi and "pdp0" when connected to 3G

也就是说,我只能使用1 x Galaxy S和1 x Galaxy S Tab进行测试,因为它们是我仅有的Android设备.我想网络接口名称是由设备管理器在内核中的某个位置设置的.我可能可以浏览每台设备的内核,但是我发现有人必须已经找到了该信息,有关在Google中查找或搜索内容的任何建议?

That said, I've only really been able to test with 1 x Galaxy S and 1 x Galaxy S Tab as they are the only Android devices I have. I imagine the network interface name is set somewhere in the kernel by the device manager. I could probably troll through the kernel for each device, but I figured someone must have found this info out already, any suggestions on where to look or what to search in google?

推荐答案

您应该使用更简单的方法.调用返回当前活动网络的 ConnectivityManager 函数- getActiveNetworkInfo() .具有 NetworkInfo 的实例,您可以调用 getType() getSubtype()来获取当前正在使用的网络类型.

You should use easier approach. Call ConnectivityManager's function that returns current active network - getActiveNetworkInfo(). Having an instance of NetworkInfo you can call getType() and getSubtype() to get network type that is currently in use.

这里是一个例子:

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设备的网络接口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆