I am doing some investigation on an app I am developing. The issue is the app requires connection to mobile data network so when wifi is on, it will not route properly since the servers are on the carrier network vs public network. Can a single app target mobile data while others fall back onto Wifi or is this something non standard?
Asked
Active
Viewed 7,289 times
4
-
Possible duplicate of [How to use 3G Connection in Android Application instead of Wi-fi?](https://stackoverflow.com/questions/2513713/how-to-use-3g-connection-in-android-application-instead-of-wi-fi) – rogerdpack Sep 23 '17 at 13:14
1 Answers
3
You can't explicitly force the communications channel on a per-app basis (you can request to use a preferred mode via ConnectivityManager.setNetworkPreference(...)
, but that's not "forcing").
Though it's probably terrible UX, you can inform the user that your app disallows use of WiFi, then disable their WiFi if they want to continue. To do so, you need the ACCESS_WIFI_STATE
and CHANGE_WIFI_STATE
permissions. The code would look something like this:
manager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
if(manager.isWifiEnabled()) {
manager.setWifiEnabled(false);
}
// and to be sure:
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);

Chris Cashwell
- 22,308
- 13
- 63
- 94
-
Good answer, OP might want to take a look at this answer which also discusses the matter: http://stackoverflow.com/a/5883882/855951. – Jean-Philippe Roy Jan 31 '12 at 20:21
-
For any recent android version you **most certainly can**. http://stackoverflow.com/a/4756630/967142 is pretty detailed, and only needs to be complemented with a connectivity state broadcast receiver to receive when high-priority mobile data has been enabled (or failed) - the example mentions "waiting" but this is sub-par and can be achieved with aforementioned broadcast receiver. – Jens Jan 31 '12 at 20:37
-
@Jens that method is spotty and only works on Android 2.2+. It requires you to essentially keep the mobile channel open by sending data every few seconds. My answer achieves exactly what the OP asked for and doesn't force his users to be on a specific version of the platform. – Chris Cashwell Jan 31 '12 at 20:58
-
The current market penetration of 2.3.3 is in excess of 50% and the share for 2.2+ is in excess of 90% - designing for 2.1 and prior is usually not anything you do if you consider ROI. – Jens Jan 31 '12 at 21:24
-