现在的位置: 首页 > 综合 > 正文

Android获取本机IP地址

2013年09月19日 ⁄ 综合 ⁄ 共 370字 ⁄ 字号 评论关闭

 private String getIp(){
     WifiManager wm=(WifiManager)getSystemService(Context.WIFI_SERVICE);
     //检查Wifi状态  
     if(!wm.isWifiEnabled())
         wm.setWifiEnabled(true);
     WifiInfo wi=wm.getConnectionInfo();
     //获取32位整型IP地址  
     int ipAdd=wi.getIpAddress();
     //把整型地址转换成“*.*.*.*”地址  
     String ip=intToIp(ipAdd);
     return ip;
 }
 private String intToIp(int i) {
     return (i & 0xFF ) + "." +
     ((i >> 8 ) & 0xFF) + "." +
     ((i >> 16 ) & 0xFF) + "." +
     ( i >> 24 & 0xFF) ;
 } 

抱歉!评论已关闭.