How to force the HTTP connection over Wi-Fi instead of cellular in Android?

前端 未结 3 1847
清歌不尽
清歌不尽 2020-12-19 20:21

My application has to detect that the device connected to the Wi-Fi network is the required one and then fire a HTTP request over that Wi-Fi network programmatically. I have

相关标签:
3条回答
  • 2020-12-19 20:37

    There is no way to force it to use one or the other. Whatever network is available is used whether 3G or Wi-Fi (if Wi-Fi is connected 3G is off). If you don't want to do something based on which network is currently in use you can do a check whether Wi-Fi is not connected and don't do some action if that is the case, and prompt the user. Google Maps does something similar to this if Latitude is enabled.

    The WifiManager class has a bunch of methods that may be useful to you.

    0 讨论(0)
  • 2020-12-19 20:39

    You can use OkHttp and a custom SocketFactory. See https://github.com/square/okhttp/issues/2404 for more details.

    0 讨论(0)
  • 2020-12-19 20:44

    you can go with setNetworkPrefrence(ConnectivityManager.TYPE_WIFI) of the ConnectivityManager class in Android

    0 讨论(0)
提交回复
热议问题