简体   繁体   English

为什么我的Android运行时进程有时会挂在waitFor()上?

[英]Why does my Android Runtime process sometimes hang at waitFor()?

I'm using a Process to ping a computer on my network. 我正在使用进程来ping网络上的计算机。 (this is in an android app that I'm working on) (这是在我正在开发的android应用中)

public int pingHost(String host) throws IOException, InterruptedException {
    String cmd = "/system/bin/ping -c 1 -W 1000 " + host;
    proc = Runtime.getRuntime().exec(cmd);
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
    String line;
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    proc.waitFor();

    input.close();
    return proc.exitValue();
}

This method is called and determines whether the ping was successful or not based on the exitValue the process provides. 调用此方法,并根据进程提供的exitValue确定ping是否成功。 With most hosts on my network that I've tested this pingHost() method with, I do get an exit value and respond with an appropriate message.(ping successful/ping failed). 使用网络上的大多数主机测试了pingHost()方法后,我确实会获得退出值并返回一条适当的消息(ping成功/ ping失败)。 But with one specific host on my network, an exitValue is NEVER returned. 但是,对于我的网络上的一个特定主机,永远不会返回exitValue。 Why does this happen? 为什么会这样?

I've found people that have come across similar issues as this, but the solutions that were provided to them haven't helped me. 我发现遇到类似问题的人,但是提供给他们的解决方案并没有帮助我。 The process still hangs. 该过程仍然挂起。 No error is provided. 没有提供错误。 Any suggestions? 有什么建议么?

I've tried adding a thread that is started before waitFor() is called... the thread sleeps for 3 seconds and then destroys the process, but this doesn't help. 我尝试添加一个在调用waitFor()之前启动的线程...该线程休眠3秒钟,然后破坏了进程,但这无济于事。 Is there a different problem that I'm not thinking of? 我没有想到其他问题吗?

Apparently a new waitFor() method was added in Java 8 that allowed a timeout parameter... my IDE is set to use jdk1.8.0_65 but this method isn't available to me. 显然,Java 8中添加了一个新的waitFor()方法,该方法允许使用超时参数...我的IDE设置为使用jdk1.8.0_65,但该方法对我不可用。 Is there something I forgot to do? 有什么我忘了做的事吗?

To answer the question of "pinging" a machine in network, you can use the: 要回答“ ping”网络中的计算机的问题,可以使用:

InetAddress.getByName(host).isReachable(timeout)

Documentation: 文档:

Tries to reach this InetAddress . 尝试到达此InetAddress This method first tries to use ICMP (ICMP ECHO REQUEST), falling back to a TCP connection on port 7 (Echo) of the remote host. 此方法首先尝试使用ICMP(ICMP ECHO REQUEST),回退到远程主机的端口7(Echo)上的TCP连接。

This function will try ping the host, and return a positive/negative result. 此功能将尝试对主机执行ping操作,并返回正/负结果。 The good thing is that this will handle most Linux/Windows/Whatever is "under the hood" of your Android device. 好消息是,这将可以处理大多数Linux / Windows / Android设备中的“幕后黑手”。 Even if you do not have access to the ping binary, or if its somewhere else in your device, and even if the path to the binary is unset. 即使您无权访问ping二进制文件,或者无法访问ping二进制文件,也无法访问ping二进制文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM