get Network Gateway from Context - Android Network

Android examples for Network:Network Operation

Description

get Network Gateway from Context

Demo Code

//  Copyright (c) 2014 Texas Instruments. All rights reserved.
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class Main{
    public static String getGateway(Context context) {
        WifiManager wm = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        return intToIp(wm.getDhcpInfo().gateway);
    }//from   w  w w .j a  v  a2  s  .c o m
    public static String intToIp(int i) {
        return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
                + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
    }
}

Related Tutorials