i want to make 3g works when wifi is enabled! Stop for a while wifi and connect through 3g. after that close 3g and work with wifi again! thanks!
Asked
Active
Viewed 304 times
0
-
Could you provide some clarification as to what your particular use case is? Off hand, I can't see any particular reason for wanting to do this. – Brent Hronik Mar 19 '13 at 16:50
-
@BrentHronik : I wrote an app which communicates with a user's home PC. Some users work in places where the wi-fi network has no route to the Internet so I added an option to temporarily shut-off wi-fi in order to switch to 3G (assuming the user had manually enabled 3G in the first place). As Waqas suggests, the mechanism to do this is not deprecated. – Squonk Mar 19 '13 at 17:53
-
@Squonk can you provide me some information for your code! because i need to do someting like your code! thanks – Cbour Mar 19 '13 at 18:11
4 Answers
0
Just disabling Wi-Fi? Both processes are automatic: connecting a Wi-Fi network when it is enabled and getting 3G when there isn't a Wi-Fi connection.

BobCormorano
- 650
- 1
- 7
- 14
-
Yes but when you are far from AP the 3g is better from wifi! and because we have the default connectivity, we can't cut off! so i need a code inside my app when i rich a threshold of bandwith to stop wifi! – Cbour Mar 19 '13 at 18:14
0
I don't think it is possible to switch to mobile data network when WiFi is enabled. Perhaps you should silently disable the WiFi (from code) and wait for the 3G network to establish. Once your required task is done, turn the WiFi on again and your 3G network will be disconnected.

waqaslam
- 67,549
- 16
- 165
- 178
-
I would disagree with this suggestion, forcing the disabling of wifi is bad UX, and is rather hacky, IMO(I actually think the ability to do so is deprecated in newer api versions). – Brent Hronik Mar 19 '13 at 16:49
-
doing so or not in terms of UX is a different debate. but anyhow this feature is [always available](http://developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)) and never been deprecated – waqaslam Mar 19 '13 at 17:39
-
Thanks for your answers! i am doing a client server app for video streaming in android phone! and i want to have access when the wifi and 3g is open. i don't want to do this by me. i want to do this automatically. because in some distances from wifi AP the 3g is better to download the chunks. So i need some help with java code to do that! if i can! thanks – Cbour Mar 19 '13 at 18:05
0
I think the following should work...
WifiManager wm = null;
WifiManager.WifiLock wfl = null;
// To disable wi-fi
wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wm != null) {
wfl = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "myWifiLock");
Boolean result = wm.setWifiEnabled(false);
Log.d(TAG, "wm.setWifiEnabled(false) result: " + result);
}
// To re-enable wi-fi
if (wfl != null)
wfl.release();
if (wm != null) {
Boolean result = wm.setWifiEnabled(true);
Log.d(TAG, "wm.setWifiEnabled(true) result: " + result);
}

Squonk
- 48,735
- 19
- 103
- 135
-
the mobisocial "Control 3G interface with your own application in Android" provide some code! this can help me for my job? thanks – Cbour Mar 19 '13 at 18:47
0
here is a code that can enable and dissable inside your code!
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Runnable runnable = new Runnable() {
// ginetai o kwdikas gia to anoigma kai to klisimo tou wifi
@Override
public void run() {
// new LongOperation().execute();
for (int i = 0; i <= 150; i++) {
try {
Thread.sleep(1000);
if (i >= 0 && i <18){
wifi.setWifiEnabled(true);
// wifi.setWifiEnabled(false);// Disabling WiFi
}
else if (i>= 18 && i < 40){
wifi.setWifiEnabled(true);
// wifi.setWifiEnabled(false); // Enabling WiFi
}
else if (i>= 40 && i < 58){
// wifi.setWifiEnabled(true); // Disabling WiFi
wifi.setWifiEnabled(false);
}
else if (i>= 58 && i < 78){
wifi.setWifiEnabled(true);
//wifi.setWifiEnabled(false); // Enabling WiFi
}
else{
// wifi.setWifiEnabled(true);
wifi.setWifiEnabled(false);// Disabling WiFi
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(runnable).start();

Cbour
- 49
- 2
- 9