Android:强制通过蜂窝网络与 WiFi 发送数据

发布于 2024-11-05 11:04:16 字数 312 浏览 0 评论 0原文

是否可以强制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浪荡不羁 2024-11-12 11:04:16

我不认为您可以在不明确暂时关闭 Wi-Fi 无线电的情况下“强制”连接路径(不推荐)。但是,您可以尝试在希望发生这种情况的期间设置网络首选项:

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);

希望有帮助!

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!

心意如水 2024-11-12 11:04:16

在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!

地狱即天堂 2024-11-12 11:04:16

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.

无声无音无过去 2024-11-12 11:04:16

(我在此处回答了同样的问题)

您不能在每个应用程序的基础上显式强制通信通道(您可以通过 ConnectivityManager 请求使用首选模式。 setNetworkPreference(...),但这不是“强制”)。

尽管这可能是糟糕的用户体验,但您可以通知用户您的应用不允许使用 WiFi,然后如果他们想继续,则禁用他们的 WiFi。为此,您需要 ACCESS_WIFI_STATECHANGE_WIFI_STATE 权限。代码看起来像这样:

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

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

(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);
油饼 2024-11-12 11:04:16

尝试看看这个,它有很多信息,可能有您正在寻找的解决方案:如何在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文