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.Enumeration;

import android.content.Context;

public class Main {
    public static NetworkInterface getActiveNetworkInterface(Context c) {
        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())
                        break; // break inner loop, continue with outer loop

                    return intf; // this is not the loopback and it has an IP address assigned
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        // nothing found
        return null;
    }
}