1. Get the phone IP address code:
public static String getlocalipaddress () {
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 ()) {return
inetaddress.gethostaddress (). toString ();
catch (SocketException e) {
//Todo:handle exception
Utils.log ("Wifipreference ipaddress---error-" + E.tostring ());
}
return null;
}
But in 4.0 there will be a similar fe80::b607:f9ff:fee5:487e IP address, this is the address of IPV6, we need to get the address of the IPV4, so to add a judgment in the appeal code
Inetaddressutils.isipv4address (Inetaddress.gethostaddress ());
2. The complete code is as follows:
public static String getlocalipaddress () {
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 () && inetaddressutils.isipv4address (Inetaddress.gethostaddress ())) { return
inetaddress.gethostaddress (). toString ();
catch (SocketException e) {
//Todo:handle exception
Utils.log ("Wifipreference ipaddress---error-" + E.tostring ());
}
return null;
}
Get the current IP address of the Android phone
public class Networkutils {/** * Check whether the network is available * * @param paramcontext * @return */public static Boolean check
Enable (Context Paramcontext) {Boolean i = false; Networkinfo Localnetworkinfo = (connectivitymanager) paramcontext. Getsystemservice ("Connectivity").
Getactivenetworkinfo ();
if (localnetworkinfo!= null) && (localnetworkinfo.isavailable ()) return true;
return false; /** * Converts the integer form of IP to IP form * * @param ipint * @return/public static String int2ip (int ipint) {Stringbuild
ER sb = new StringBuilder ();
Sb.append (Ipint & 0xFF). Append (".");
Sb.append ((Ipint >> 8) & 0xFF). Append (".");
Sb.append ((Ipint >>) & 0xFF). Append (".");
Sb.append ((Ipint >>) & 0xFF);
return sb.tostring ();
/** * Get Current IP Address * * @param context * @return/public static String getlocalipaddress 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 ()) {//Return inetaddress.gethostaddress (). toString ();
}//}//} Wifimanager Wifimanager = (wifimanager) context. Getsystemservice (Context.wifi_service);
Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();
int i = wifiinfo.getipaddress ();
return Int2ip (i); The catch (Exception ex) {return] gets an IP error bird!!!!
Please make sure it is WiFi, or please reopen network!\n "+ ex.getmessage ();
}//return null; }
}
Get native IP address and MAC address in Android
by Inetaddress.getlocalhost () is always "127.0.0.1", to get the real network IP address through the following method:
First create a new project, modify the Androidmanifest.xml file to increase user rights, as follows:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>//must write
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>//must write
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission>
The main function code is as follows:
Get native IP address public string Getlocalhostip () {String ipaddress = "";
try {enumeration<networkinterface> en = networkinterface. getnetworkinterfaces (); Traverse the network interface used while (En.hasmoreelements ()) {NetworkInterface nif = en.nextelement ();//Get all IP enums bound by each network interface
Eration<inetaddress> inet = nif.getinetaddresses ();
Traverses all IP while (inet.hasmoreelements ()) {inetaddress IP = inet.nextelement () for each interface binding;
if (!ip.isloopbackaddress () && inetaddressutils.isipv4address (IP. gethostaddress ())) {
return ipaddress = "The IP of this machine is" + ":" + ip.gethostaddress ();
catch (SocketException e) {log.e ("Feige", "Get local IP address failed");
E.printstacktrace ();
return IPAddress;
//Get native MAC address public string Getlocalmac () {String mac = "";
Get WIFI manager Wifimanager wifimng = (wifimanager) getsystemservice (Context.wifi_service);
Wifiinfo wifiinfor = Wifimng.getconnectioninfo (); MAC = "The MAC address of this machine is:" + wifiinfor.getmacaddress ();
return mac; }
Android access to wifi IP address
Wifimanager wifimanage= (Wifimanager) Context.getsystemservice (context.wifi_service);//Get WifiManager
// Check to see if WiFi opens if
(!wifimanage.iswifienabled ()) {
wifimanage.setwifienabled (true);
}
Wifiinfo wifiinfo= wifimanage.getconnectioninfo ();
String Ip=inttoip (Wifiinfo.getipaddress ());
Convert the obtained int to a real IP address, refer to the Web, modify the next
private String inttoip (int i) {return
(I & 0xFF) + "." + ((I >> 8) &A mp 0xFF)? + "." + ((I >>) & 0xFF) + "." + ((i >>) & 0xFF);
OK, is that good? Oh, don't forget to add permission
Copy Code code as follows:
<uses-permission= "" Android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses>
<uses-permission= "" Android:name= "Adnroid.permission.CHANGE_WIFI_STATE" ></uses>
Summary: You can compare, Android access to mobile phone IP address method, so as not to create unnecessary problems in the process of programming.