Android:强制通过蜂窝网络与 WiFi 发送数据
是否可以强制 Android 应用程序仅使用移动无线电连接(3g/4g/等),而不允许使用 WiFi?
我想我想使用 HIPRI 连接:(例如:WIFI 打开,使用 HIPRI 3G): http://groups.google.com/group/android-developers/browse_thread /线程/d41f85505484d29b
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为您可以在不明确暂时关闭 Wi-Fi 无线电的情况下“强制”连接路径(不推荐)。但是,您可以尝试在希望发生这种情况的期间设置网络首选项:
希望有帮助!
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:
Hope that Helps!
在Android 2.2中,您可以在使用WiFi的同时使用高优先级移动数据。 “feature”参数的值为“enableHIPRI”,并且隐藏在电话 API 中。
方法:
ConnectivityManager.startUsingNetworkFeature(int networkType, String feature) 的
http://developer.android.com/reference/android/net/ConnectivityManager。 html
来源:http://code.google.com/p/android/issues/detail?id=5885
您可以查看其他答案:https://stackoverflow.com/a/4756630/327011
这不是一个好的政策...如果确实需要,请使用它!
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() 即将被废弃。但更重要的是,如果您在更改之前执行 getNetworkPreference(),它将返回 ConnectivityManager.TYPE_MOBILE。设置在那里没有任何区别。至于 HIPRI 本身,它的工作模式相当奇怪。首先,它允许连接到所有主机,而不仅仅是那些明确请求的主机。其次,当您使用 stopUsingFeature...() 调用将其关闭时,它不会被关闭并且仍处于活动状态。第三,即使 wifi 可用,所有设备应用程序也会开始使用它,这与文档中的内容相矛盾。
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.
(我在此处回答了同样的问题)
您不能在每个应用程序的基础上显式强制通信通道(您可以通过
ConnectivityManager 请求使用首选模式。 setNetworkPreference(...)
,但这不是“强制”)。尽管这可能是糟糕的用户体验,但您可以通知用户您的应用不允许使用 WiFi,然后如果他们想继续,则禁用他们的 WiFi。为此,您需要
ACCESS_WIFI_STATE
和CHANGE_WIFI_STATE
权限。代码看起来像这样:(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
andCHANGE_WIFI_STATE
permissions. The code would look something like this:尝试看看这个,它有很多信息,可能有您正在寻找的解决方案:如何在 Android 应用程序中使用 3G 连接而不是 Wi-fi?
它有工作服务示例,可设置 HIPRI 移动连接并保持其运行。据我所知,这是在 Android 中使用 wifi 和 3g 的唯一或多或少简单的方法。唯一的缺点是,此连接只允许将数据传输到明确请求路由的服务器。这基本上意味着您无法路由到许多服务器,因为按名称查找主机以获取 IP 地址需要时间,并且为 10 台服务器执行此操作将需要 30-60 秒,这使其启动缓慢。因此,您应该确切地知道哪些服务器可以通过移动连接使用。
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.