问题描述
我一直努力做的事情我认为将是主机测试在我的应用程序的互联网企业开始一个简单的可达性,但文件心不是帮助也不是在不同的地方发现的例子,香港专业教育学院尝试了很多的解决方案,没有运气,所以如果任何人都可以点我的一个明确的方法来与Android检查主机的可用性是真棒方向,只需要它切换一个布尔为true,如果主机可以达到
Ive been trying to do what i thought would be a simple reachability of host test at the beginning of my apps internet ventures, but documentation isnt helping and neither are examples found at various places, ive tried many solutions with no luck, so if anyone could point me in the direction of a definitive way to check a hosts availability with android that be awesome, just need it to toggle a bool to true if the host can be reached
使用API8如果使多大差别这个过程中,并且必须满足无根设备,以使inetaddress.isReachable超出即时
im using API8 if that makes much difference to this process, and must cater for non-rooted devices so the inetaddress.isReachable is out
推荐答案
这不是pretty的,但是这是我做的:
It's not pretty but this is how I did it:
boolean exists = false;
try {
SocketAddress sockaddr = new InetSocketAddress(ip, port);
// Create an unbound socket
Socket sock = new Socket();
// This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
exists = true;
}catch(Exception e){
}
这篇关于检查主机是否可达/可用性在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!