如何判断Android网络连接情况?
如何确定 Android 设备当前可用的互联网连接类型?例如让它返回 WiFi、3G、无。
How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用它来确定您是否已连接:
此代码需要权限
ACCESS_NETWORK_STATE
和ACCESS_WIFI_STATE
。编辑:
public NetworkInfo getNetworkInfo(int networkType)
已在 API 21 中弃用。对于高于 20 的 API 级别,您可以执行以下操作:我根据 OP 的问题将此代码限制为 Wi-Fi 和 3G,但很容易将其扩展到新添加的连接类型,例如以太网或 Wi-Max。
You can use this to determine whether you are connected:
This code requires the permissions
ACCESS_NETWORK_STATE
andACCESS_WIFI_STATE
.EDIT:
public NetworkInfo getNetworkInfo (int networkType)
has been deprecated in API 21. For API levels higher than 20 you can do as follows:I limited this code to Wi-Fi and 3G as per the OP's question but it's easy to extend it to newly added connection types such as Ethernet or Wi-Max.
不知道为什么您的应用程序崩溃,也许这有帮助。也许尝试这个方法:(需要应用程序上下文)
然后尝试调用
希望这会有所帮助
Don't know why your app crashes, maybe this helps. Maybe try this method: (needs the application context)
then try to call
hope this helps
看到这个帖子,我回答了同样的问题
如何在 Android 应用程序中保存请求并重新发送< /a>
see this post, I answered the same question
how to save request and resend in android app
您还可以使用 TelephonyManager 来了解移动网络类型是:
可能的网络类型列于 http:// developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29
You can also use the TelephonyManager to find out what the mobile network type is:
Possible network types are listed at http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29