如何检查 Android 中的互联网连接?
我想检查每个活动中的互联网连接。如果丢失,应显示一条消息。
任何人都可以指导我如何实现这一目标吗?
I want to check the internet connectivity in each activity. If it is lost a message should be displayed.
Can any one guide me how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
任一点只能有一个连接处于活动状态。因此,一个更简单的答案是:
它还适合任何新型网络,例如 ConnectivityManager#TYPE_WIMAX
还要确保您拥有监视网络状态所需的权限。您需要将此权限添加到 AndroidManifest.xml 中:
Only one connection can be active at any one point. So a simpler answer is:
It also caters for any new type of network such as ConnectivityManager#TYPE_WIMAX
Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:
您可以使用 ConnectivityManager 检查网络状态。
请注意,常量 ConnectivityManager.TYPE_MOBILE 和 ConnectivityManager.TYPE_WIFI 表示连接类型,这两个值并不详尽。有关详尽列表,请参阅此处。
另请确保您拥有监控网络状态所需的权限。您需要将此权限添加到 AndroidManifest.xml 中:
You can use the ConnectivityManager to check the network state.
Note that the constants ConnectivityManager.TYPE_MOBILE and ConnectivityManager.TYPE_WIFI represent connection types and these two values are not exhaustive. See here for an exhaustive list.
Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:
对于各种类型的网络状态都可以这样做
You can do this, for various types of network status
来检查移动和 Wi-Fi 的网络覆盖和数据可用性
您可以直接通过查看网络覆盖可用性、
查看数据可用性(如果网络可用)
You can check network coverage and data availability of Mobile and wi-fi directly with
For Network coverage availability,
For Data availability if network available
修正
应该是
Correction
should be
注册广播接收器来处理CONNECTIVITY_ACTION。请参阅此完整内容示例。您应该更新一个静态变量“connectionAvailable”,该变量每次都可以通过其各自的 getter 进行访问。
请记住在 manifest 文件中声明广播接收器:
关于“签入每个活动”的问题,您可能有兴趣使用由您的活动扩展的 BaseActivity管理连接测试并显示消息。
另请注意,使用事件(而不是从活动轮询)会更加高效。
Register a broadcast receiver to handle CONNECTIVITY_ACTION. See this full example. You should update a static variable 'connectionAvailable' that will be accessible everywhere and everytime through its respective getter.
Remember to declare the broadcast receiver in the manifest file:
On the matter of 'checking in each activity', may be you would be interested in using a BaseActivity extended by your activities and managing the test of connectivity and displaying the message.
Also, note that using events (not polling from activities) will be more efficient.
您没有正确使用
ConnectivityManager.getNetworkInfo(0).getState()
和ConnectivityManager.getNetworkInfo(1).getState()
,而不是硬编码值 (1) 和(0) 使用ConnectivityManager.TYPE_WIFI
和ConnectivityManager.TYPE_MOBILE
You are not using
ConnectivityManager.getNetworkInfo(0).getState()
andConnectivityManager.getNetworkInfo(1).getState()
properly, instead of hardcoding the values (1) and (0) useConnectivityManager.TYPE_WIFI
andConnectivityManager.TYPE_MOBILE
这是一个布尔检查,用于查看您是否具有网络访问权限。它不会确定哪种网络访问 - 移动、wifi...,它只是检查您是否在线。
This is a boolean check to see if you have network access. It doesn't determine what kind of network access - mobile, wifi..., it just checks to see if you're online.