安卓 获取手机IP地址的实现代码
程序员文章站
2024-03-05 10:40:00
1.获取手机ip地址的代码:
public static string getlocalipaddress(){
try{
for (enume...
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 地址的方法,以免在编程的过程中造成不必要的问题.