Just wanted to add a bit to the discussion. I've been using bindProcessToNetwork for a while and it does fix the loss of WiFi when connected to one without a network as described by Shawn Chen. This is a heavy handed solution that makes it difficult to use Mobile network at the same time. However, the routing layer is rather broken on Android somewhere around 6 where they started forcing request to mobile even though the routing table indicates the wifi device.
Anyway, you can actually bind at a socket level which I describe below. This may help when the process has been bound to the WiFi network and you still want to use mobile. Instead of looking up WiFi (which is what I was experimenting with instead of binding the process), you look up the mobile 'Network' object and build a socket to it.
What we need is something that indicates to android not to drop the WiFi connection.
HttpUrlConnection doesn't allow you to get the socket so you can use okhttp to do it at the per-connection level. I'm using 2.7.5 of OkHttp but it is very similar for the 3+ version of OkHttp.
You get the current networks then process each to find WiFi network.
Network[] networks = connectivityManager.getAllNetworks();
Then you build the url like:
String url = "http://someurl";
HttpUrlConnection uconn = new OkUrlFactory( new OkHttpClient().setSocketFactory(network.getSocketFactory())).open(url)
This all works fine for a few connections and sometimes it works for the series of connections I used. Android is using the knowledge that activity is happening over the network but any pauses and it jumps back to using mobile. Android 9 wifi is just flawed and haven't figured out how to make it sticky using the socket solution. There may be one.
automatic activation of Wi-Fi
??? You are connected with wifi to that accespoint arent you? So wifi is activated already. – Luncheon