简体   繁体   中英

Set app to use cellular network rather than WiFi in android 5+

We have an app we are making that needs to switch to cellular for some requests even when WiFi is connected. According to the ConnectionManager documentation these following methods are now deprecated, but is not so clear on what to use instead.

  public void useMobileNetworkMode(Context context) {
    ConnectivityManager  cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
}

public void useDefaultNetworkMode(Context context) {
    ConnectivityManager  cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE);

}

Do these methods still work in android 5/6? and if anyone has info or something to replace these methods with I would be very grateful!


Ok little bit more on the problem :) I have managed to investigate ConnectivityManager and can see the networks using :

                     Network networkToUse = null;
    Network[] networks;
    networks = cm.getAllNetworks();
    for (Network network : networks) {
        NetworkInfo ni = cm.getNetworkInfo(network);
        Log.e("NETWORKINFO", ni.getType() + "   " + ni.getExtraInfo());
        if (ni.getType()== ConnectivityManager.TYPE_WIFI) {
            Log.e("NETWORKINFO", "isWifi");
            if (ni.isConnected()) {
                Log.e("NETWORKINFO", "and is connected");
                if (networkToUse == null) {
                    networkToUse = network;
                }
            }
        }
        if (ni.getType()== ConnectivityManager.TYPE_MOBILE) {
            Log.e("NETWORKINFO", "HasMobile");
            if (ni.isConnected()) {
                Log.e("NETWORKINFO", "and is connected");
                networkToUse = network;
            }
        }
    }

It is here I kind of get stuck because I can't logically see a way of telling the app to use one of these networks when performing a HttpsURLConnection from URL.openLink(); .


UPDATE: I have just noticed that the mobile one disappears shortly after WiFi connects. There is a moment where I get both but not for long.

I have also tried this:

    final ConnectivityManager connection_manager =
            (ConnectivityManager)httpsClient.getCheckoutController().getCheckout().getCurrentActivity().getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);

    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {

        @Override
        public void onAvailable(Network network) {
            Log.e("NETWORKINFO", "FOUND A CELLULAR NETWORK " + connection_manager.getNetworkInfo(network));
        }
    });

    request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {


        @Override
        public void onAvailable(Network network) {
            Log.e("NETWORKINFO", "FOUND A WIFI NETWORK "+connection_manager.getNetworkInfo(network));
        }
    });

but as with the "list" of networks" in previous try I only ever 1 callback, even if mobile data is on as well.


UPDATE; Ok I seem to see mobile sometimes using above method. but it seems to create API level spaghetti hell. It sometimes also takes a very long time for the mobile callback to fire. I wonder if this is because it has to wake up the cellular modem and wait for it's handshake or something?


UPDATE; I had another possible suggestion using Sockets (something I have little experience with..)

Does anyone know if it is possible to build a CELLULAR SSL connection socket to do https requests using HttpsURLConnection.getSocketFactory() and SSLSocket ?

Any info would be very welcome in this week long quest :D


UPDATE: Found a good and categorical answer from someone at google: How to stay connected through mobile network after WIFI is connected on Android?

However after implementation, i get a network callback for the mobile but when i open URL connection and perform a request it seems to get stuck for ages (about 4 minutes) before i get the response.

I have a Huawei 5.0.1 phone, which is the highest i have available. Obviously this is not good. However it is not tested on 5.1 galaxy S6 and works.. so could be the phone.

According to the Android documentation it is no longer working in Android version 5 and above.

This method was deprecated in API level 21.

Functionality has been removed as it no longer makes sense, with many more >than two networks - we'd need an array to express preference. Instead we >use dynamic network properties of the networks to describe their >precedence.

Found a good and categorical answer from someone at google: How to stay connected through mobile network after WIFI is connected on Android?

(Link is in edited info above)

However after implementation, i get a network callback for the mobile but when i open URL connection and perform a request it seems to get stuck for ages (about 4 minutes) before i get the response.

I have a Huawei 5.0.1 phone, which is the highest i have available. Obviously this is not good. However it is not tested on 5.1 galaxy S6 and works.. so could be the phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM