Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
最近看了好多網(wǎng)上獲取IP地址的例子,發(fā)現(xiàn)好多都不完全準(zhǔn)確,這里我寫一下獲取ip地址的兩種方式。
比如微信支付,后臺在做接口的時(shí)候,要求App端傳入IP地址,我們需要判斷是網(wǎng)絡(luò)環(huán)境,WI-FI還是3G,所以需要獲取這兩種環(huán)境的ip地址。
第一步:首先是判斷網(wǎng)絡(luò)環(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)絡(luò),第二步,獲取本地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)絡(luò),第三步,獲取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準(zhǔn)確獲取手機(jī)IP地址的兩種方式,更多關(guān)于Android獲取手機(jī)IP地址的方式請查看下面的相關(guān)鏈接
相關(guān)文章
Fragment 多層嵌套方法調(diào)用問題的解決方案
這篇文章主要介紹了Fragment 多層嵌套方法調(diào)用問題的解決方案的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android開發(fā)實(shí)現(xiàn)刪除聯(lián)系人通話記錄的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)刪除聯(lián)系人通話記錄的方法,較為詳細(xì)的分析了Android刪除通話記錄的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)定時(shí)自動靜音小助手
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)自動靜音小助手,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
深入Android HandlerThread 使用及其源碼完全解析
這篇文章主要介紹了深入Android HandlerThread 使用及其源碼完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
Flutter質(zhì)感設(shè)計(jì)之底部導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之底部導(dǎo)航的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android實(shí)現(xiàn)放大鏡效果的方法實(shí)例(附源碼)
這篇文章主要給大家介紹了利用Android實(shí)現(xiàn)放大鏡效果的方法實(shí)例,文中給出了詳細(xì)的介紹和示例代碼,文章的結(jié)尾更是給出了源碼供大家下載學(xué)習(xí),有需要的朋友們下面來一起看看吧。2017-01-01
Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
本篇文章主要介紹了Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10

