3

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 searched, but I didn't find any method or class which will help me to do this.

I thought of disabling the cellular network so that the HTTP request will go through the Wi-Fi network but even in this I didn't have success. And also, can I enable and disable the cellular and Wi-Fi networks programatically?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amsiddh
  • 3,513
  • 2
  • 24
  • 27
  • Doesn't the device do that automatically anyways? And the other question is, should you really decide for the user, which internet connection he wants to use? – markus Mar 02 '11 at 18:50
  • Thnaks markus for ur reply, I got what ur trying to say but u dint got it clear wht i need. (As when we do http connection requests the android framework will decide from which network it should go through depending on there availability.) But its all my appln requirement to have http conn requets to go throught wifi only not cellular. The appln is abt wifi hotspots, say when user is in "abc" wifi network place then starts the app do authentication via http request through "abc" wifi network. Then after susscess allow user to use "abc" wifi network for browsing. – amsiddh Mar 02 '11 at 19:13
  • Why not just handle it all server side like most wifi ISP's do? Then you get something that works on any device, doesn't require installation, tracking Android versions, etc. – Chris Stratton Mar 02 '11 at 20:13
  • Hey chris, i agree with you..its all client requirement and they already having server side implementation as similar to other wifi hotspot providers and now they also want to have native app. – amsiddh Mar 04 '11 at 07:16

3 Answers3

3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Thanks robby for ur reply, ok that's true..if both wifi and cellular network is available the os will use only one network but which is on highest priority is wifi or cellular i.e. [will wifi on(highest priority) and cellular off] or viceversa. How do i make sure the request is going from wifi only. – amsiddh Mar 02 '11 at 19:28
  • @amsiddh Read my answer again. If wifi is connected the only data connection available is wifi, 3g will be inactive. – Robby Pond Mar 02 '11 at 19:35
  • hey robby thanks once again, will implements this and will get back with its results. – amsiddh Mar 02 '11 at 19:45
2

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

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
  • Note that the application requires the CHANGE_NETWORK_STATE permission in order to be able to do this. I fiddled around with this a bit and it appears the call (once it takes effect) appears to affect all applications that use connectivity so this must surely be used with care. I also found that this call is unreliable (at least on the Nexus One I tried it on) – scorpiodawg Jun 09 '11 at 00:26
  • setNetworkPrefrence(ConnectivityManager.TYPE_WIFI) is deprecated in API level 21. See more: https://developer.android.com/reference/android/net/ConnectivityManager.html#setNetworkPreference(int) – krawiec Jul 04 '16 at 09:05
0

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

mbonnin
  • 6,893
  • 3
  • 39
  • 55