- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGetIPAddressUtil.java
More file actions
Latest commit
53 lines (45 loc) · 1.67 KB
/
Copy pathGetIPAddressUtil.java
File metadata and controls
53 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
packagecom.momo.mvp_demo;
importandroid.content.Context;
importandroid.net.wifi.WifiInfo;
importandroid.net.wifi.WifiManager;
importandroid.util.Log;
importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.util.Enumeration;
/**
* Created by RealMo on 2016-12-23.
*/
publicclassGetIPAddressUtil {
publicstaticStringgetWifiIP(Contextcontext) {
Stringip = null;
WifiManagerwifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
//需要在Manifest声明权限
WifiInfowifiInfo = wifiManager.getConnectionInfo();
inti = wifiInfo.getIpAddress();
ip = (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}
returnip;
}
publicstaticStringgetMobileIP() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterfaceintf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddressinetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
returninetAddress.getHostAddress().toString();
}
}
}
} catch (SocketExceptionex) {
Log.e("哎呀,出错了...", ex.toString());
}
returnnull;
}
}