get GPRS Local Ip Address - Android Network

Android examples for Network:IP Address

Description

get GPRS Local Ip Address

Demo Code


//package com.java2s;

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

public class Main {

    public static String getGPRSLocalIpAddress() {
        try {/*from   w ww.j  a  v  a  2s  .  c  o  m*/
            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();
                    }
                }
            }
        } catch (SocketException ex) {
            //            DebugUtils.d(ex.getMessage());
        }
        return "";
    }
}

Related Tutorials