Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public static String getIpAddress() {
  2. try {
  3. for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
  4. NetworkInterface intf = (NetworkInterface) en.nextElement();
  5. for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
  6. InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
  7.  
  8. if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
  9. String ipAddress = inetAddress.getHostAddress().toString();
  10. Log.e("IP address", "" + ipAddress);
  11. return ipAddress;
  12. }
  13. }
  14. }
  15. } catch (SocketException ex) {
  16. Log.e("MASOOM", ex.toString());
  17. }
  18. return null;
  19. }
  20.  
  21. public String getIpAddress(){
  22. WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
  23. int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
  24. return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement