Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

import android.util.Log;

public class Main {
    private static String TAG = "Utilities";

    public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
        try {
            HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();

                String name = intf.getName();
                ArrayList<String> addresses = new ArrayList<String>();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        addresses.add(inetAddress.getHostAddress().toString());
                    }
                }
                result.put(name, addresses);
            }
            return result;
        } catch (SocketException ex) {
            Log.e(TAG, ex.toString());
        }
        return null;
    }
}