对于ConnectivityManager.NetworkCallback的onAvailable或广播ConnectivityManager.CONNECTIVITY_ACTION网络状态连接不代表获取到了ip,可以打印ip查看是否获取到ip,获取到ip后才能访问互联网
public static String readIfconfig() {
StringBuilder ifconfigOutput = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("ifconfig");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
ifconfigOutput.append(line).append("\n");
}
reader.close();
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return ifconfigOutput.toString();
}
可以使用adb shell logcat | grep Dhcp查看Dhcp客户端获取的ip的过程