Java Code Examples for android.text.format.Formatter#formatIpAddress()

The following examples show how to use android.text.format.Formatter#formatIpAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From RPiCameraViewer with MIT License 6 votes vote down vote up
public static String getLocalIpAddress()
{
	String address = "";
	WifiManager manager = (WifiManager)App.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	if (manager.isWifiEnabled())
	{
		WifiInfo wifiInfo = manager.getConnectionInfo();
		if (wifiInfo != null)
		{
			NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
			if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
			{
				int ip = wifiInfo.getIpAddress();
				address = Formatter.formatIpAddress(ip);
			}
		}
	}
	return address;
}
 
Example 2
Source File: BadIntentPreferencesActivity.java    From BadIntent with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //try to set this WiFi IP (in case there is no [valid] value set yet)
    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    String wifiIP = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    SharedPreferences sPrefs = getSharedPreferences(AppAnalyzer.PREFNAME, MODE_WORLD_READABLE);
    String target_ip = sPrefs.getString(BadIntentConstants.TARGET_IP, " ");
    if (target_ip.equals(" ") || target_ip.equals("0.0.0.0")) {
        sPrefs.edit()
                .putString(BadIntentConstants.TARGET_IP, wifiIP)
                .apply();
    }
    addPreferencesFromResource(R.xml.bad_intent_preferences);

}
 
Example 3
Source File: LocalNetwork.java    From AndroidDemo with MIT License 5 votes vote down vote up
/**
 * 创建服务端
 *
 * @param context 上下文
 * @param handler 处理来自客户端的请求
 * @return LocalNetwork
 */
public static LocalNetwork createServer(Context context, ServerHandler handler) {
    ServerNetwork network = new ServerNetwork(handler);
    WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    String address = Formatter.formatIpAddress(wifi.getConnectionInfo().getIpAddress());
    network.setSelfIp(address);
    return new LocalNetwork(network);
}
 
Example 4
Source File: Wifi.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static String getIpAddress(final Context context) {
    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (manager == null) return null;

    WifiInfo wifiInfo = manager.getConnectionInfo();

    // TODO: Formatter.formatIpAddress() is deprecated! Replace it...
    return (wifiInfo == null) ? null : Formatter.formatIpAddress(wifiInfo.getIpAddress());
}
 
Example 5
Source File: NetworkUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the net mask by wifi.
 *
 * @return the net mask by wifi
 */
@RequiresPermission(ACCESS_WIFI_STATE)
public static String getNetMaskByWifi() {
    @SuppressLint("WifiManagerLeak")
    WifiManager wm = (WifiManager) Utils.getApp().getSystemService(Context.WIFI_SERVICE);
    if (wm == null) return "";
    return Formatter.formatIpAddress(wm.getDhcpInfo().netmask);
}
 
Example 6
Source File: NetworkUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the gate way by wifi.
 *
 * @return the gate way by wifi
 */
@RequiresPermission(ACCESS_WIFI_STATE)
public static String getGatewayByWifi() {
    @SuppressLint("WifiManagerLeak")
    WifiManager wm = (WifiManager) Utils.getApp().getSystemService(Context.WIFI_SERVICE);
    if (wm == null) return "";
    return Formatter.formatIpAddress(wm.getDhcpInfo().gateway);
}
 
Example 7
Source File: WifiActivity.java    From PMCADemo with MIT License 5 votes vote down vote up
protected void wifiConnected() {
    WifiInfo info = wifiManager.getConnectionInfo();
    String ssid = info.getSSID();
    String ip = Formatter.formatIpAddress(info.getIpAddress());
    log(ssid + ": Connected. Server URL: http://" + ip + ":" + HttpServer.PORT + "/");
    webView.loadUrl("https://www.google.com/");
}
 
Example 8
Source File: NetworkUtils.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
public static void getGatewayIpAddress(Context c) {
    // get wifi ip

    final WifiManager manager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    final DhcpInfo dhcp = manager.getDhcpInfo();
    final String address = Formatter.formatIpAddress(dhcp.gateway);

    StringBuilder IFCONFIG = new StringBuilder();
    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() && !inetAddress.isLinkLocalAddress()
                        && inetAddress.isSiteLocalAddress()) {
                    IFCONFIG.append(inetAddress.getHostAddress() + "\n");
                }

            }
        }
    } catch (SocketException ex) {
        Log.e("LOG_TAG", ex.toString());
    }
    MLog.d(TAG, "ifconfig " + IFCONFIG.toString());

    MLog.d(TAG, "hotspot address is " + address);

}
 
Example 9
Source File: NetworkUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static String getGatewayIP(Context context) {
    try {
        return Formatter.formatIpAddress(((WifiManager) context.getSystemService(NETWORK_NAME_WIFI)).getDhcpInfo().gateway);
    } catch (Throwable e) {
        LogTool.e(TAG, "", e);
        return "";
    }
}
 
Example 10
Source File: NetWorkUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 根据 Wifi 获取网络 IP 地址
 * @return 网络 IP 地址
 */
@RequiresPermission(android.Manifest.permission.ACCESS_WIFI_STATE)
public static String getIpAddressByWifi() {
    try {
        @SuppressLint("WifiManagerLeak")
        WifiManager wifiManager = AppUtils.getWifiManager();
        return Formatter.formatIpAddress(wifiManager.getDhcpInfo().ipAddress);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getIpAddressByWifi");
    }
    return null;
}
 
Example 11
Source File: EnvironmentUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取本机连接WiFi网络的IP地址
 */
public static String getSelfIp(@NonNull Context context){
    try{
        WifiManager wifiManager=(WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return Formatter.formatIpAddress(wifiManager.getDhcpInfo().ipAddress);
    }catch (Exception e){e.printStackTrace();}
    return "0.0.0.0";
}
 
Example 12
Source File: Util.java    From mirror with MIT License 5 votes vote down vote up
/**
 * Shows a {@link Toast} with the IPv4 address of the Wifi connection. Useful for debugging,
 * especially when using adb over Wifi.
 */
public void showIpAddress() {
  Context appContext = context.getApplicationContext();
  WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  String ipAddress = null;
  if (wifiInfo != null) {
    ipAddress = Formatter.formatIpAddress(wifiInfo.getIpAddress());
  }
  if (ipAddress == null) {
    ipAddress = context.getString(R.string.unknown_ip_address);
  }
  Toast.makeText(context, ipAddress, Toast.LENGTH_LONG).show();
}
 
Example 13
Source File: NetWorkUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 根据 Wifi 获取网关 IP 地址
 * @return 网关 IP 地址
 */
@RequiresPermission(android.Manifest.permission.ACCESS_WIFI_STATE)
public static String getGatewayByWifi() {
    try {
        @SuppressLint("WifiManagerLeak")
        WifiManager wifiManager = AppUtils.getWifiManager();
        return Formatter.formatIpAddress(wifiManager.getDhcpInfo().gateway);
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "getGatewayByWifi");
    }
    return null;
}
 
Example 14
Source File: LocalWebServer.java    From android-robocar with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static String getIpAddress(Context context) {
  WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}
 
Example 15
Source File: WifiFragmentSupport.java    From zhangshangwuda with Apache License 2.0 4 votes vote down vote up
public static String FormatIP(int IpAddress) {
	return Formatter.formatIpAddress(IpAddress);
}
 
Example 16
Source File: VinylCastHelpers.java    From vinyl-cast with MIT License 4 votes vote down vote up
public static String getIpAddress(Context context) {
    WifiManager wm = (WifiManager) context.getSystemService(WIFI_SERVICE);
    return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}
 
Example 17
Source File: NetworkUtil.java    From mirror with Apache License 2.0 4 votes vote down vote up
private static String getIpAddressFromWifi(final Context context) {
    final WifiManager manager = (WifiManager) context.getSystemService(WIFI_SERVICE);
    return Formatter.formatIpAddress(manager.getConnectionInfo().getIpAddress());
}
 
Example 18
Source File: SubnetScanner.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void run() {
    int ipAddress = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getIpAddress();
    if (ipAddress != 0) {
        tryWithBroadcast();
        String formatIpAddress = Formatter.formatIpAddress(ipAddress);
        String substring = formatIpAddress.substring(0, formatIpAddress.lastIndexOf(46) + 1);
        if (!isInterrupted()) {
            for (ipAddress = 0; ipAddress < 100; ipAddress++) {
                this.tasks.add(this.pool.submit(new Task(substring + ipAddress)));
                this.tasks.add(this.pool.submit(new Task(substring + (ipAddress + 100))));
                if (ipAddress < 56) {
                    this.tasks.add(this.pool.submit(new Task(substring + (ipAddress + 200))));
                }
            }
            while (!this.tasks.isEmpty()) {
                int size = this.tasks.size();
                int i = 0;
                while (i < size) {
                    if (!isInterrupted()) {
                        try {
                            Computer computer = (Computer) ((Future) this.tasks.get(i)).get(1, TimeUnit.MILLISECONDS);
                            this.tasks.remove(i);
                            size--;
                            if (computer.name != null) {
                                //SELog.d("SMB host found at ", computer.addr);
                                onFound(computer);
                            } else {
                                //SELog.d("No SMB host found at ", computer.addr);
                            }
                            ipAddress = size;
                        } catch (InterruptedException e) {
                            return;
                        } catch (ExecutionException e2) {
                            Throwable th = e2;
                            ipAddress = size;
                          //  SELog.w(th);
                        } catch (TimeoutException e3) {
                            ipAddress = size;
                        }
                        i++;
                        size = ipAddress;
                    } else {
                        return;
                    }
                }
            }
            try {
                this.bdThread.join();
            } catch (InterruptedException e4) {
            }
        } else {
            return;
        }
    }
    synchronized (this.mLock) {
        if (this.observer != null) {
            this.observer.searchFinished();
        }
    }
    this.pool.shutdown();
}
 
Example 19
Source File: ClientService.java    From LocalNetwork with MIT License 4 votes vote down vote up
private String getLocalIp() {
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}
 
Example 20
Source File: MainActivity.java    From mini-hacks with MIT License 4 votes vote down vote up
/** Get local IP to display in a TextView on the P2P tab */
public String getLocalIpAddress() {
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}