您的位置:首页 > 移动开发 > Android开发

如何检查Android上的互联网访问? InetAddress永远不会超时

2020-07-14 06:54 1086 查看

精选30+云产品,助力企业轻松上云!>>>

问题:

I got a AsyncTask that is supposed to check the network access to a host name. 我有一个AsyncTask应该检查网络访问主机名。 But the doInBackground() is never timed out. 但是doInBackground()永远不会超时。 Anyone have a clue? 有人有线索吗?

public class HostAvailabilityTask extends AsyncTask<String, Void, Boolean> {

private Main main;

public HostAvailabilityTask(Main main) {
this.main = main;
}

protected Boolean doInBackground(String... params) {
Main.Log("doInBackground() isHostAvailable():"+params[0]);

try {
return InetAddress.getByName(params[0]).isReachable(30);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

protected void onPostExecute(Boolean... result) {
Main.Log("onPostExecute()");

if(result[0] == false) {
main.setContentView(R.layout.splash);
return;
}

main.continueAfterHostCheck();
}
}

解决方案:

参考一: https://stackoom.com/question/6Y20/如何检查Android上的互联网访问-InetAddress永远不会超时
参考二: https://oldbug.net/q/6Y20/How-to-check-internet-access-on-Android-InetAddress-never-times-out
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android got