Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在获取自己设备的格式化IP地址_Android - Fatal编程技术网

Android 正在获取自己设备的格式化IP地址

Android 正在获取自己设备的格式化IP地址,android,Android,我试过这个密码 公共类MainActivity扩展了活动{ private static final String TAG = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv= (TextView) findV

我试过这个密码 公共类MainActivity扩展了活动{

private static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv= (TextView) findViewById(R.id.textView2);
    String a=getLocalIpAddress();
      tv.setText(a);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public String getLocalIpAddress() {
      try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                     String a= inetAddress.getHostAddress();

                     return a;
                    }
                }
            }
        } catch (SocketException ex) {
           // Log.e(tag, ex.toString());
        }
        return "";
    }
}
私有静态最终字符串标记=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.textView2);
字符串a=getLocalIpAddress();
tv.setText(a);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共字符串getLocalIpAddress(){
试一试{
对于(枚举en=网络接口
.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举枚举enumIpAddr=intf
.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
字符串a=inetAddress.getHostAddress();
返回a;
}
}
}
}捕获(SocketException例外){
//Log.e(tag,例如toString());
}
返回“”;
}
}
这给我的ip地址像00ff:22ff:。。。。 但我想要192.168.3.3。。。 格式ip地址已折旧
是否有其他方法可以获取格式化地址

您获取的地址是IPv6地址。您试图获得的是IPv4,因此请尝试使用:

if ((!inetAddress.isLoopbackAddress()) && (InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress()))) { ... }
而不是:

if (!inetAddress.isLoopbackAddress()) { ... }

你得到的是一个物理MAC地址,而不是IP地址。你有没有看过这十几个相同问题的答案?@CiaranBaselman,没有,他得到的是IPv6地址。那么请接受这个问题,这两个问题都是为了声誉,但总的来说是为了让其他有相同问题的用户知道此解决方案对您有效。请不要回答已经回答过的问题,尤其是一次以上的问题。此外,您的答案只是部分,新进程将只找到第一个ip4地址,而不是环回地址。在具有多个接口(即wifi和4G)的设备上,这可能不会返回活动连接。