Android: Force data to be sent over radio vs WiFi

2019-01-10 23:27发布

Is it possible to force an Android application to use only the mobile radio connection (3g/4g/etc), disallowing the use of WiFi?

I think I want to use a HIPRI connection: (ex: WIFI turned on, use HIPRI 3G): http://groups.google.com/group/android-developers/browse_thread/thread/d41f85505484d29b

5条回答
\"骚年 ilove
2楼-- · 2019-01-11 00:20

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!

查看更多
Animai°情兽
3楼-- · 2019-01-11 00:20

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.

查看更多
在下西门庆
4楼-- · 2019-01-11 00:21

(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);
查看更多
贼婆χ
5楼-- · 2019-01-11 00:30

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!

查看更多
贼婆χ
6楼-- · 2019-01-11 00:30

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.

查看更多
登录 后发表回答