WiFi monitoring on Android device

I need a suggestion on handling multiple Wi-Fi scenarios in my application.

Suppose my application is running and the device is idle for a while, and it goes into sleep mode. In standby mode, Wi-Fi is automatically turned off. Now, when the device returns from standby mode, it takes some time to turn on the Wifi state. Now, if during this time the background service application makes an HTTP request, how do we handle this script?

OR

Suppose that if Wifi is not available, the device tries to connect to GPRS / 3G, and if during this period, if the user / help desk makes an http request, how do we process it in code?

+4
source share
1 answer
  • You can specify the network interfaces, their type and connection status:

    Get all network interfaces:

    NetworkInfo[] networkInfos = ConnectivityManager.getAllNetworkInfo(); 

    check network interface type

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

    check NetworkInfo.State status

     networkInfo.getState() == NetworkInfo.State.CONNECTED 
  • If you need a connection, you can:

    a. Check the network status as described above.

    b. Recipient registration for update information when changing a network connection: Action for network events in android sdk

  • If you try to get HTTP data when there is no connection, you will get an exception. You can catch this and repeat later, but option 2. is better.

+2
source

All Articles