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

泪湿孤枕 提交于 2020-07-25 01:10:00

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!