如何确定互联网连接当前是否可用并且在 android 设备上处于活动状态?

2022-01-24 00:00:00 connection android java

我有这个需要通过互联网从远程服务器加载数据的 android 应用程序.这个更新功能显然要求设备不仅要连接到某种网络,还要连接到互联网.

so:希望将更新服务安排到某个日期,当它启动时,它应该确定它是否真的可以到达目标服务器.因此,一个简单的设备是否连接到 wifi?"还不够,因为该设备可能连接到不提供互联网访问的无线网络.需要 PING 之类的东西...

确定是否有可用的互联网连接(即服务器是否可达)最简单/最好的方法是什么?

解决方案

看看这个 回答 在 StackOverflow 上.它应该正是您正在寻找的.

i have this android application that requires to load data from a remote server via the internet. this update-functionaliy obviously requires the device to not only be connected to some kind of network, but also to the internet.

so: is want to schedule the update-service to some date and when it starts, it should determine whether it actually CAN reach the target server or not. therefore, a simple "is the device connected to wifi?" does not suffice, as the device may be connected to a wireless network that does not offer internet access. something like a PING is required...

whats the easiest / best way to determine, if there is an internet connection is available i.e. server is reachable?

解决方案

Take a look at this answer here on StackOverflow. It should be just what you are looking for.

相关文章