Programowo uzyskiwanie szczegółów bramy i maski podsieci
Jak mogę uzyskać szczegóły maski bramy i podsieci w Androidzie?
7 answers
Znalazłem klasę o nazwie DhcpInfo
w pakiecie android.net
. Posiada kilka zmiennych publicznych, które przechowują wartości bieżących parametrów sieci. Ale problem polega na tym, że zwracają wartość w liczbie całkowitej przekonwertowanej z 8Bit przesuniętych binarnych.
Przykładowy obrazek opisujący scenariusz:
* * * * Oto przykładowy kod: * * *
**plik java: * * * *
package com.schogini.dhcp;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.net.*;
import android.net.wifi.WifiManager;
public class dhcpInfo extends Activity {
public String s_dns1 ;
public String s_dns2;
public String s_gateway;
public String s_ipAddress;
public String s_leaseDuration;
public String s_netmask;
public String s_serverAddress;
TextView info;
DhcpInfo d;
WifiManager wifii;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
d=wifii.getDhcpInfo();
s_dns1="DNS 1: "+String.valueOf(d.dns1);
s_dns2="DNS 2: "+String.valueOf(d.dns2);
s_gateway="Default Gateway: "+String.valueOf(d.gateway);
s_ipAddress="IP Address: "+String.valueOf(d.ipAddress);
s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);
s_netmask="Subnet Mask: "+String.valueOf(d.netmask);
s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);
//dispaly them
info= (TextView) findViewById(R.id.infolbl);
info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);
}
}
Kodowanie Xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.schogini.dhcp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".dhcpInfo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
Próbowałem przekonwertować wartość całkowitą na jej odpowiednik, ale jeśli to zrobisz, możesz odpisać.. Pa..
Aktualizacja: trochę jak udało się przekonwertować Format IP do V4 z formularza integer Konwersja do formatu IPv4:
public String intToIp(int i) {
return ((i >> 24 ) & 0xFF ) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF) ;
}
Zdjęcie dzięki uprzejmości: http://www.bennadel.com/blog/1830-Converting-IP-Addresses-To-And-From-Integer-Values-With-ColdFusion.htm
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-31 14:51:20
Formatowanie.formatIpAddress (int) jest przestarzały i nie chcemy używać przestarzałych metod, prawda?
Wersja AndroidKid jest jakoś odwrócona, ale to powinno to naprawić:
public String intToIp(int addr) {
return ((addr & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF) + "." +
((addr >>>= 8) & 0xFF));
}
Źródło: http://www.devdaily.com/java/jwarehouse/android/core/java/android/net/DhcpInfo.java.shtml
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-17 19:14:36
Aby sformatować ip, spróbuj użyć:
import android.text.format.Formatter;
public String FormatIP(int IpAddress)
{
return Formatter.formatIpAddress(IpAddress);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-30 06:34:33
Użyj Formatter.formatIpAddress(mask);
maska jest Twoim int.
String maskk = Formatter.formatIpAddress(mask);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-29 08:42:34
Zamiast dostać 255.255.255.0, po prostu zmień kolejność w zamian;) więc będziesz mógł dostać się w odpowiedniej kolejności...
public String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 24 ) & 0xFF) ;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-30 10:54:27
To jest stary wątek, ale znalazłem oficjalną funkcję używaną przez android API (Pakiet android.net.NetworkUtils
):
/**
* Convert a IPv4 address from an integer to an InetAddress.
* @param hostAddress an int corresponding to the IPv4 address in network byte order
*/
public static InetAddress intToInetAddress(int hostAddress) {
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
try {
return InetAddress.getByAddress(addressBytes);
} catch (UnknownHostException e) {
throw new AssertionError();
}
}
A gdy już masz InetAddress
, możesz uzyskać sformatowany ciąg znaków w ten sposób:
intToInetAddress(d.gateway).getHostAddress()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-11 07:48:55
Choć stare, działa jak urok.
TvGateway.setText(Formatter.formatIpAddress (dhcpInfo.gateway));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-17 01:57:34