Everything practically works, with one exception. If we first assign transmission through mobile data, then everything works well, the data goes through the mobile network. Then I choose wifi data, everything is also good. But if you choose mobile data again after that, then the transfer is already going through wifi, why so? Why does all OK work at the first start, and when wifi is turned on again, data goes through wifi, and not through mobile communication? Who knows the reason? Thank you in advance. Code below.
public void setTransportType(int transportType) {
connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (networkCallback != null) {
connectivityManager.unregisterNetworkCallback(networkCallback);
networkCallback = null;
}
NetworkRequest.Builder request = new NetworkRequest.Builder();
if (transportType == TRANSPORT_TYPE_WIFI) {
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
} else {
request.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
}
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
connectivityManager.bindProcessToNetwork(null);
connectivityManager.bindProcessToNetwork(network);
/*Operation after switching over*/
}
};
connectivityManager.requestNetwork(request.build(), networkCallback);
}