安卓系统强制应用程序使用移动数据通道[英] Android force app to use mobile data channel

本文是小编为大家收集整理的关于安卓系统强制应用程序使用移动数据通道的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在对我正在开发的应用进行一些调查.问题是应用程序需要连接到移动数据网络,因此当WiFi打开时,由于服务器位于运营商网络与公共网络上,它不会正确地路由.可以单个应用程序目标移动数据,而其他应用程序则重新返回WiFi,或者是非标准的东西吗?

推荐答案

您无法在每个应用程序的基础上显式强制通信通道(您可以通过 ConnectivityManager.setNetworkPreference(...) ,但这不是"forcing").

虽然它可能是可怕的ux,但是你 can 告诉用户你的应用程序禁止使用wifi,然后如果他们想继续,则禁用他们的wifi.为此,您需要ACCESS_WIFI_STATE和CHANGE_WIFI_STATE权限.代码看起来像这样:

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

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

本文地址:https://www.itbaoku.cn/post/102353.html

问题描述

I am doing some investigation on an app I am developing. The issue is the app requires connection to mobile data network so when wifi is on, it will not route properly since the servers are on the carrier network vs public network. Can a single app target mobile data while others fall back onto Wifi or is this something non standard?

推荐答案

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