最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)準確獲取手機IP地址的兩種方式

 更新時間:2020年03月19日 16:22:07   作者:willA書歡  
這篇文章主要介紹了Android開發(fā)準確獲取手機IP地址的兩種方式,需要的朋友可以參考下

最近看了好多網(wǎng)上獲取IP地址的例子,發(fā)現(xiàn)好多都不完全準確,這里我寫一下獲取ip地址的兩種方式。

比如微信支付,后臺在做接口的時候,要求App端傳入IP地址,我們需要判斷是網(wǎng)絡環(huán)境,WI-FI還是3G,所以需要獲取這兩種環(huán)境的ip地址。

第一步:首先是判斷網(wǎng)絡環(huán)境:

String ip;
ConnectivityManager conMann = (ConnectivityManager) 
				this.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		if (mobileNetworkInfo.isConnected()) {
			ip = getLocalIpAddress();	
			System.out.println("本地ip-----"+ip);
		}else if(wifiNetworkInfo.isConnected())
		{
			WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
		  int ipAddress = wifiInfo.getIpAddress();  
		  ip = intToIp(ipAddress); 
		  System.out.println("wifi_ip地址為------"+ip);
		}	

如果連接的是移動網(wǎng)絡,第二步,獲取本地ip地址:getLocalIpAddress();這樣獲取的是ipv4格式的ip地址。

public String getLocalIpAddress() { 
    try { 
      String ipv4; 
      ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); 
      for (NetworkInterface ni: nilist)  
      { 
        ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses()); 
        for (InetAddress address: ialist){ 
          if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))  
          {  
            return ipv4; 
          } 
        } 
  
      } 
  
    } catch (SocketException ex) { 
      Log.e("localip", ex.toString()); 
    } 
    return null; 
  } 

如果連接的是WI-FI網(wǎng)絡,第三步,獲取WI-FI ip地址:intToIp(ipAddress);

public static String intToIp(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(); 
	  } 

網(wǎng)上的很多代碼獲取的是ipv6的本地ip,在微信支付里這種ip地址無法調(diào)起微信支付,附代碼:

private String getlocalIp() {
	String ip;
	
		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()&&!inetAddress.isLinkLocalAddress()) { 
//	  	        	ip=inetAddress.getHostAddress().toString();
						System.out.println("ip=========="+inetAddress.getHostAddress().toString());
						return inetAddress.getHostAddress().toString(); 
					 
					} 
				} 
			} 
		} catch (SocketException ex) { 
			Log.e("WifiPreference IpAddress", ex.toString()); 	  	  
	} 
	return null;
}

本文主要介紹了Android準確獲取手機IP地址的兩種方式,更多關于Android獲取手機IP地址的方式請查看下面的相關鏈接

相關文章

最新評論

汝南县| 北票市| 扎鲁特旗| 延长县| 渝北区| 丹凤县| 普格县| 延津县| 德惠市| 漯河市| 屯昌县| 元阳县| 博湖县| 马鞍山市| 朝阳县| 大荔县| 墨脱县| 青河县| 永和县| 石城县| 琼结县| 济阳县| 博湖县| 保亭| 宝坻区| 北辰区| 固阳县| 阿拉善左旗| 平泉县| 新邵县| 丰台区| 泗水县| 宁海县| 衡阳县| 盐亭县| 武邑县| 盖州市| 友谊县| 盘山县| 泽库县| 华蓥市|