TrendIpUtils.java
3.21 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.glsx.libxj.util;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@SuppressWarnings("deprecation")
public class TrendIpUtils {
public static String getIp(Context context) {
//获取wifi服务
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
// StringBuilder sb = new StringBuilder();
// sb.append("网络信息:");
// sb.append("\nipAddress:" + intToIp(dhcpInfo.ipAddress));
// sb.append("\nnetmask:" + intToIp(dhcpInfo.netmask));
// sb.append("\ngateway:" + intToIp(dhcpInfo.gateway));
// sb.append("\nserverAddress:" + intToIp(dhcpInfo.serverAddress));
// sb.append("\ndns1:" + intToIp(dhcpInfo.dns1));
// sb.append("\ndns2:" + intToIp(dhcpInfo.dns2));
// sb.append("\n");
// sb.append("Wifi信息:");
// sb.append("\nIpAddress:" + intToIp(wifiInfo.getIpAddress()));
// sb.append("\nMacAddress:" + wifiInfo.getMacAddress());
LogUtils.e("网址信息====" + intToIp(dhcpInfo.gateway));
// int ipAddress = wifiInfo.getIpAddress();
// String ip = intToIp(ipAddress);
return intToIp(dhcpInfo.gateway);
}
private static String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
(i >> 24 & 0xFF);
}
public static String getGateWayIp() {
String ip = "";
try {
Process process = Runtime.getRuntime().exec("ip route list table 0");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String str = in.readLine();
while (!TextUtils.isEmpty(str)){
String[] array = str.split("\\s+");
if (array != null && array.length > 4){
if ("usb0".equals(array[4])){
ip = array[2];
return ip;
}else if ("wlan0".equals(array[4])){
ip = array[2];
}
}
str = in.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return ip;
}
public static int getUid(Context context) {
int uid = 200;
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo("com.glsx.libxj", PackageManager.GET_META_DATA);
uid = ai.uid;
} catch (PackageManager.NameNotFoundException e) {
LogUtils.e(e.getMessage());
}
return uid;
}
}