Search code examples
javaandroidnetwork-programmingwifi4g

Change type of Internet connection


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?


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;