在安卓开发里,要求做一款可以实现ping功能的app
时间: 2023-05-14 16:06:27 浏览: 71
可以使用 Java 中的 InetAddress 类来实现 ping 功能。以下是一个简单的示例代码:
```
public class PingApp {
public static void main(String[] args) {
String ipAddress = "www.google.com";
try {
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending ping request to " + ipAddress);
if (inet.isReachable(5000)) {
System.out.println(ipAddress + " is reachable");
} else {
System.out.println(ipAddress + " is not reachable");
}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
```
这个程序会尝试连接到指定的 IP 地址,并在 5 秒内等待响应。如果连接成功,则说明该 IP 地址是可达的。如果连接失败,则说明该 IP 地址不可达。