Search code examples
androidandroid-wifiandroid-networking

Send data only through mobile data on android phones when wifi is on


Is it possible to programmatically route a request through mobile data even when the wifi is on and connected to the internet?

My app needs to call a service provided by the operator which is only available through mobile data and I don't think it's user-friendly to ask for turning off the wifi.


Solution

  • take a look at https://developer.android.com/reference/android/net/ConnectivityManager.html

    specifically, it has a function that allows you to do what you want.

     5 Provide an API that allows applications to request and select networks for their data traffic
    

    you request a network, and then setDefaultNetwork( API before 23) or use bindProcessToNetwork for API >= 23 .

    you probably want to request a network with int TRANSPORT_CELLULAR capabilities (https://developer.android.com/reference/android/net/NetworkCapabilities.html)