6

As far as I know, the hardware/os decides which network will execute my app's requests, if both MobileData and WiFi networks are on, judging by this answer here: In Android, if both mobile and wifi networks are available, which one is used?.

This comment here says the same: https://android.stackexchange.com/questions/57710/how-do-you-determine-if-youre-using-wi-fi-or-lte#comment79856_57919

Can I instruct my network requests to only use WiFi, even when both WiFi and Data networks are available?

I am trying to solve this problem: The user has a setting that says "Do NOT use mobile data" which should forbid the app from sending network requests if Mobile Data is on (which can be subject of charges).

However, if that answer to my linked question is really true, that means that if both Mobile and WiFi are on, WiFi is not guaranteed to take precedence over Mobile and Mobile will still perform requests and the user will be charged.

At the moment in my code I check what type of network connection is available, and if that setting is on ("Do NOT use mobile data") I only perform requests if WiFi is ON and MobileData is OFF. If both are ON, I do NOT perform requests in order to protect the user from being charged.

However, it is difficult explaining this to my colleagues, so I am thinking - is it possible to instruct my app's network requests to be only performed via WiFi, even if both WiFi and MobileData are ON simultaneously?

PS: I just found an exactly opposite opinion, stating that if both MobileData and WiFi are on, all requests will be performed via WiFi: https://android.stackexchange.com/questions/17774/how-can-i-tell-that-my-phone-uses-the-wi-fi-connection-for-data-traffic

I am not sure who to believe.

EDIT: I found some code snippets that are supposed to do this job: Send request over Mobile data when WIFI is ON.(Android L) but I don't know how to test them and if they really do what I want to do here.

How to use data connection instead of WIFI when both are enabled?

Community
  • 1
  • 1
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • I've always been under the assumption that if both WiFi and Mobile are enabled and available, the device will default to WiFi for all network requests. That's the way my device has always worked (I never see the "using data" animation in my notification bar unless WiFi is unavailable). You might check out the accepted answer on this question, as it seems to confirm this: http://stackoverflow.com/questions/5171770/how-to-force-the-http-connection-over-wi-fi-instead-of-cellular-in-android – NoChinDeluxe Jan 18 '16 at 17:46
  • I have changed my answer to provide two links to questions in SO whose answers claim that it is possible, but they use very strange and unpopular APIs – Kaloyan Roussev Jan 18 '16 at 20:34
  • It should be quite safe to assume that if both networks are available, wifi will be used for your network requests. This is simply how Android devices work by default. This would simply require you to test to make sure wifi is available before making your call (and blocking it otherwise). While it could be possible for a user to force mobile usage even when they are connected to wifi, that is not a problem you should have to worry about. Your responsibility, in that case, would be to warn the user that they should be connected to wifi before using said feature. – NoChinDeluxe Jan 18 '16 at 21:19
  • @J.K. did you ever find solution to this? What I'd like to do is a bit different. Namely I want to connect to Wifi network and send request through Wifi network (internal request) that doesn't have internet even if 4G is enabled. – Erti-Chris Eelmaa May 18 '17 at 12:09

0 Answers0