简体   繁体   English

监控Android设备上的WiFi

[英]Monitoring the WiFi on android device

I need suggestion on handling few Wifi scenarios in my application. 我需要在我的应用程序中处理一些Wifi方案的建议。

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. 在睡眠模式下,wifi会自动关闭。 Now again when the device is brought back from the sleep mode it takes some time to turn on the Wifi state. 现在,当设备从睡眠模式恢复时,需要花费一些时间才能打开Wifi状态。 Now if during this time app background service is making http request, how we handle that scenario? 现在,如果在这段时间内应用后台服务正在发出http请求,我们如何处理这种情况?

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? 假设如果Wifi不可用,则设备正在尝试连接到GPRS / 3G,并且在此期间,如果用户/后台服务发出了HTTP请求,我们如何在代码中处理它?

  1. You can list network interfaces, their type and connectivity status: 您可以列出网络接口,它们的类型和连接状态:

    Get all network interfaces: 获取所有网络接口:

     NetworkInfo[] networkInfos = ConnectivityManager.getAllNetworkInfo(); 

    check type of network interface 检查网络接口的类型

     networkInfo.getType() == ConnectivityManager.TYPE_MOBILE // or TYPE_WIFI 

    check status against NetworkInfo.State 根据NetworkInfo.State检查状态

     networkInfo.getState() == NetworkInfo.State.CONNECTED 
  2. If you need a connection then you can: 如果需要连接,则可以:

    a. 一种。 Check the network status as described above. 如上所述,检查网络状态。

    b. b。 Register a Receiver to get update info when network connectivity changes: Intent action for network events in android sdk 注册接收器以在网络连接更改时获取更新信息: Android SDK中网络事件的意图操作

  3. If you try to get a HTTP data when there is no connectivity you will get an exception. 如果在没有连接的情况下尝试获取HTTP数据,则会出现异常。 You can catch this and retry later, but option 2. is better. 您可以捕获此错误,然后稍后重试,但是选项2更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM