Question

Is there a way to change the Internet connection type ? For example when the device is connected with WLAN/Wifi my App still wants to use the 3G/4G connection. Can I handle that?

Was it helpful?

Solution

Add the following to the manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

Then when your app needs to use 3G/4G do the following:

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled())
{
    wifiManager.setWifiEnabled(false);
}

and import the following in any file you are using the above:

import android.net.wifi.WifiManager;

OTHER TIPS

first of all you need this in your Android Manifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

Then you can turn the WiFi off like this:

    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    else
    {
        wifiManager.setWifiEnabled(true);
    }

There is no way to use 3g connection if WiFi is enabled since the device will use WiFi if it's enabled instead of 3g. It's just how the Android system handles internet connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top