• 安卓中获取ip地址


    • 获取当前手机的ip地址

        public static String getIPAddress(Context context) {
        NetworkInfo info = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                    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() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }
      
            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
                return ipAddress;
            }
        } else {
            //当前无网络连接,请在设置中打开网络
        }
        return null;
        }
      
                    /**
                    * 将得到的int类型的IP转换为String类型
                    *
                    * @param ip
                    * @return
                    */
      
                    public static String intIP2StringIP(int ip) {
                        return (ip & 0xFF) + "." +
                        ((ip >> 8) & 0xFF) + "." +
                        ((ip >> 16) & 0xFF) + "." +
                        (ip >> 24 & 0xFF);
                        }
      
    • 获取设备的ip地址

      • 原理是遍历所有网络接口的所有IP地址。如果方法返回null,则设备没有可用的网络连接。方法返回的IP地址是设备正在使用的IP地址。

          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()) {
                          return inetAddress.getHostAddress().toString();
                      }
                  }
              }
          } catch (SocketException ex) {
          Log.e(LOG_TAG, ex.toString());
          }
          return null;
          }
  • 相关阅读:
    hdu1072 逃离迷宫系列 bfs
    hdu1495 倒水bfs
    hdu 1548 A strange lift (bfs)
    hdu1728 逃离迷宫bfs
    hdu1548 奇怪的电梯 dfs dijkstra bfs都可以,在此奉上dfs
    delphi 窗体的位置和高宽度-TForm:Letf、Top、Width、Height、ClientWidth、ClientHeight
    Delphi 鼠标控制函数GetCursorPos、SetCursorPos
    Delphi CoCreateGuid()函数 获取GUID
    Delphi出现“borland license information was found,but it is not valid for delphi”的错误,无法运行的解决方法
    一维条形码生成与识别技术
  • 原文地址:https://www.cnblogs.com/xxc0505/p/6825699.html
Copyright © 2020-2023  润新知