安卓 获取手机IP地址的实现代码
1.获取手机IP地址的代码:
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;
}
但是在4.0 下 会出现类似fe80::b607:f9ff:fee5:487e的IP地址, 这个是IPV6的地址,我们需要获得是的IPV4的地址,所以要在上诉代码中加一个判断
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress());
2.完整代码如下:
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;
}
获取android手机当前ip地址
public class NetWorkUtils {
/**
* 检查网络是否可用
*
* @param paramContext
* @return
*/
public static boolean checkEnable(Context paramContext) {
boolean i = false;
NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
.getSystemService("connectivity")).getActiveNetworkInfo();
if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable()))
return true;
return false;
}
/**
* 将ip的整数形式转换成ip形式
*
* @param ipInt
* @return
*/
public static String int2ip(int ipInt) {
StringBuilder sb = new StringBuilder();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
/**
* 获取当前ip地址
*
* @param context
* @return
*/
public static String getLocalIpAddress(Context context) {
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);
} catch (Exception ex) {
return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage();
}
// return null;
}
}
Android中获取本机ip地址和MAC地址
通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法:
首先新建一个工程,修改AndroidManifest.xml文件增加用户权限,如下:
<uses-permission android:name="android.permission.INTERNET"/> //必写
<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>//必写
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
主要函数代码如下:
// 得到本机ip地址
public String getLocalHostIp()
{
String ipaddress = "";
try
{
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
// 遍历所用的网络接口
while (en.hasMoreElements())
{
NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
Enumeration<InetAddress> inet = nif.getInetAddresses();
// 遍历每一个接口绑定的所有ip
while (inet.hasMoreElements())
{
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress()))
{
return ipaddress = "本机的ip是" + ":" + ip.getHostAddress();
}
}
}
}
catch (SocketException e)
{
Log.e("feige", "获取本地ip地址失败");
e.printStackTrace();
}
return ipaddress;
}
// 得到本机Mac地址
public String getLocalMac()
{
String mac = "";
// 获取wifi管理器
WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
return mac;
}
Android 获取wifi的IP地址
WifiManager wifimanage=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);//获取WifiManager
//检查wifi是否开启
if(!wifimanage.isWifiEnabled()) {
wifimanage.setWifiEnabled(true);
}
WifiInfo wifiinfo= wifimanage.getConnectionInfo();
String ip=intToIp(wifiinfo.getIpAddress());
//将获取的int转为真正的ip地址,参考的网上的,修改了下
private String intToIp(int i) {
return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );
}
OK,这样就好了吗?呵呵,别忘记加上权限
<uses -permission="" android:name="android.permission.ACCESS_WIFI_STATE"></uses>
<uses -permission="" android:name="adnroid.permission.CHANGE_WIFI_STATE"></uses>
总结:大家可以对比一下,Android 获取手机 IP 地址的方法,以免在编程的过程中造成不必要的问题.
以上是 安卓 获取手机IP地址的实现代码 的全部内容, 来源链接: utcz.com/z/325434.html