tags:

views:

70

answers:

3

Using the Connectivity Manager Class We can get access to either wifi or Internet Network
Using
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    // ARE WE CONNECTED TO THE NET
     if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) 

where 0 and 1 respectively refers to mobile and wifi connection

If my android device is connected to both .. can we switch between any of the network or can we disable any of the network..

Like using a function connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED);

Thanks

A: 

I know of enabling or disabling wifi:

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

wifiManager.setWifiEnabled(status);

where status may be true or false as per requirement.

viv
A: 

Yes it do enable or disable the wifi very well. But a problem associated is that i have both the wifi connectivity and mobile connectivty on my android device..
i made a sample app for testing the active network
By default if the device is connected to both wifi and mobile network, the device first prefer the wifi connectivty for connecting , if wifi is disabled then it goes for the mobile network...i have both network connected on my device if in the starting of app i disable the wifi using the above code then the device looses the internet connectivty not even the mobile connectivity....which was there...after closing the app if i check my wifi enabled status then it is off as it is supposed, and then if i restart application again then it get the connectivity to mobile network

Javanator
A: 

Found The solution ..it took some time to shift between networks... So Now if we disable the wifi it automatically get connected to mobile network after few seconds.. and if we enable the wifi then it get connected to wifi network again... The main thread in my application is checking the connectivity before that shift...

Thanks for your answer

Javanator