Android: Force data to be sent over radio vs WiFi

牧云@^-^@ 提交于 2019-11-27 12:12:27

I don't believe you can "force" the connection path without explicitly turning off the Wi-Fi radio temporarily (not recommended). However, you could try setting the network preference during the period you want this to occur:

ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//Prefer mobile over wifi
cm.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);

//Do your work

//Remove your preference
cm.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE);

Hope that Helps!

neteinstein

In Android 2.2 you can use high-priority mobile data at the same time as WiFi. The value of the "feature" parameter is "enableHIPRI", and is hidden in the Phone API.

Method: ConnectivityManager.startUsingNetworkFeature(int networkType, String feature) of http://developer.android.com/reference/android/net/ConnectivityManager.html

Source: http://code.google.com/p/android/issues/detail?id=5885

You could check this other answer: https://stackoverflow.com/a/4756630/327011

This is NOT a good policy...use it if REALLY needed!

ConnectivityManager.setNetworkPreference() is close to be obsoleted. But what is more important, if you do getNetworkPreference() before changing, it will return ConnectivityManager.TYPE_MOBILE. Setting it there does not make any difference. As for HIPRI itself, it works in pretty strange mode. First, it allows connections to all hosts, not only to those explicitly requested. Second, when you turn it off with stopUsingFeature...() call, it will not be turned off and will be still active. Third, all device applications start using it even if wifi is available, which contradicts to what is said in documentation.

Chris Cashwell

(I answered this same question here)

You can't explicitly force the communications channel on a per-app basis (you can request to use a preferred mode via ConnectivityManager.setNetworkPreference(...), but that's not "forcing").

Though it's probably terrible UX, you can inform the user that your app disallows use of WiFi, then disable their WiFi if they want to continue. To do so, you need the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions. The code would look something like this:

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

if(manager.isWifiEnabled()) {
    manager.setWifiEnabled(false);
}
// and to be sure:
ConnectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
Cynichniy Bandera

Try to look at this, it has a lot of info and may have solution you are looking for: How to use 3G Connection in Android Application instead of Wi-fi?

It has working service example that sets up the HIPRI mobile connection and keeps it running. AFAIK this is the only more or less straightforward way to have working wifi and 3g in android. The only drawback is that, this connection will allow only data transmission to servers that routing was explicitly requested to. This basically means that you cannot route to many servers because lookup host by name to get ip address takes time and doing it for 10 servers will take 30-60 sec, which makes it start slowly. So you should know exactly what servers should be available via mobile connection.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!