views:

1171

answers:

2

How to use 3G Connection in Android Application instead of Wi-fi?

I want to connect a 3G connection, is there any sample code to connect to 3G instead of Wi-fi?

+2  A: 

I think that is not possible from Java. The system shuts down all mobile network based communication if connected to a wireless network. I think that you aren't allowed to start a 3G connection from you program.

Janusz
thank q if the mobile user enable both means 3G and WiFi that time my application running failed because of it is continuously jumping WiFi to 3G and 3G to WiFi can u pl give any alternative solution
Aswan
There is no way around this. If the user has a very spoty 3G connection she will get network and loose network connection all the time too. The only thing you can do is do small and frequent requests to your server and don't try to keep a connection open for a longer time. And then handle the network errors in an appropriate way maybe retrying it or advising the user to try it later with a more stable connection.
Janusz
+2  A: 

The T-Mobile 'My Account" app does this, if you are connected to a WiFi connection it tells you that their program will not work over WiFi and then asks the user if they want to turn off the WiFi connection. If you choose "No" then the application exits, if you choose "Yes" then the app turns off your WiFi connection and then continues with starting up.

I think this is a good model to follow, it will ensure that your app is not being ran over WiFi and allows the user to decide if they want to turn off WiFi or not. An improvement on this model would be to turn wifi back on when the user navigates away from your app.

I haven't tested the following code, but it looks like it should work (modified from here)

use the following permissions in your manifest

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

and here is some actual code to turn wifi on/off

private WifiManager wifiManager;

@Override 
public void onCreate(Bundle icicle)
{
    ....................

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

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

If you do not want to go down that route it looks like you might be able to tell the phone that you would prefer to use the mobile data network rather than the wifi network.

The Android ConnectivityManager offers a function setNetworkPreference. This function is not really documented as you can tell if you click the link. I would paly around with it though because the constants that are defined seem to hint that you can set this to either TYPE_MOBILE or TYPE_WIFI and there is a DEFAULT_NETWORK_PREFERENCE constant as well that is defined as being 0x00000001 which is the same as TYPE_WIFI. So try getting access to a ConnectivityManager by calling

Context.getSystemService(Context.CONNECTIVITY_SERVICE);

and then try using the setNetworkPreference() function.

It doesn't appear to require any permissions in the manifest but it might require the CHANGE_NETWORK_STATE permission or something along those lines.

If you do sue the setNetworkPreference function it would probably be best to also set the Network Preference back to its original values (received from getNetworkPreference)

I hope this helps.

snctln