监控 Android 设备上的 WiFi
我需要关于处理我的应用程序中的一些 Wifi 场景的建议。
假设我的应用程序正在运行并且设备保持空闲一段时间并且已进入睡眠模式。在睡眠模式下,wifi会自动关闭。现在,当设备从睡眠模式恢复时,需要一些时间才能打开 Wifi 状态。 现在,如果在此期间应用程序后台服务正在发出 http 请求,我们如何处理这种情况?
或者
假设如果 Wifi 不可用,设备正在尝试连接到 GPRS/3G,如果在此期间用户/后台服务正在发出 http 请求,我们如何在代码中处理它?
I need suggestion on handling few Wifi scenarios in my application.
Suppose my application is running and device is kept idle for a while and it has gone into sleep mode. In the sleep mode the wifi is automatically turned off. Now again when the device is brought back from the sleep mode it takes some time to turn on the Wifi state.
Now if during this time app background service is making http request, how we handle that scenario?
OR
suppose if Wifi is not available, device is trying to connect to the GPRS/3G and if during this period if user/background service is making the http request, how we handle it in the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以列出网络接口、其类型和连接状态:
获取所有网络接口:
检查网络接口类型
根据
NetworkInfo.State
检查状态如果您需要连接,那么您可以:
a.按照上述方法检查网络状态。
b.注册接收器以在网络连接发生变化时获取更新信息:网络的意图操作android sdk 中的事件
如果您在没有连接的情况下尝试获取 HTTP 数据,您将收到异常。您可以捕获此问题并稍后重试,但选项 2. 更好。
You can list network interfaces, their type and connectivity status:
Get all network interfaces:
check type of network interface
check status against
NetworkInfo.State
If you need a connection then you can:
a. Check the network status as described above.
b. Register a Receiver to get update info when network connectivity changes: Intent action for network events in android sdk
If you try to get a HTTP data when there is no connectivity you will get an exception. You can catch this and retry later, but option 2. is better.