gpt4 book ai didi

android - 即使我有互联网也无法 ping 站点

转载 作者:太空狗 更新时间:2023-10-29 14:58:49 26 4
gpt4 key购买 nike

我正在尝试 ping 一个站点以检测我的 Android 应用程序是否有互联网。在许多设备上这确实运行良好,但今天,使用新设备时,它总是返回我没有互联网。

这是代码:

    static public  boolean isInternetAvailable(String url){
Runtime runtime = Runtime.getRuntime();
try {
String command = "/system/bin/ping -c 1 " + url;
Process ipProcess = runtime.exec(command);
int exitValue = ipProcess.waitFor();
return (exitValue == 0);

} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }

return false;
}

即使服务器在线,并且我可以从各种其他设备 ping 它,但在新设备上它总是返回 false。有没有可能是因为某种原因 ping 命令被阻止了?新设备是三星 GT-19305,Android 版本为 4.3。

最佳答案

你可以试试这个:

Process process = new ProcessBuilder()
.command("/system/bin/ping", "android.com")
.redirectErrorStream(true)
.start();
try {
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();

readStream(in);

finally {
process.destroy();
}
}

关于android - 即使我有互联网也无法 ping 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28852396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com