问题描述
Android Lollipop
似乎默认为移动数据.有人知道这是否在某处有正式记录吗?
Android Lollipop
seems to default to Mobile Data when the Wi-Fi
you are connected to has no Internet access. Does anybody know if this is officially documented somewhere?
我们有一个应用程序需要通过 Wi-Fi
连接到没有互联网的机器.我们的客户现在报告 Wi-Fi
连接不再工作,因为手机会自动切换到 LTE
.
We have an application that needs to connect to machines via Wi-Fi
that do not have Internet. Our customers are now reporting that the Wi-Fi
connection does not work anymore, because the phone automatically switches to LTE
.
我的理解是手机仍然保持Wi-Fi
连接,但使用LTE
除了提供对互联网的访问(lollipop-feature-spotlight-android-now-defaults-to-mobile-data-when-wi-fi-has-no-internet-access-signal-icon-adds-a-for-no-connection).
My understanding would be that the phone still keeps the Wi-Fi
connection but uses LTE
in addition to provide access to the Internet (lollipop-feature-spotlight-android-now-defaults-to-mobile-data-when-wi-fi-has-no-internet-access-signal-icon-adds-a-for-no-connection).
我对这个功能的理解有错吗?如果是这样,有没有办法在没有互联网的情况下强制使用Wi-Fi?我在开发人员文档中找不到任何关于此的内容.
Is my understanding of this feature wrong? And if so, is there a way to force using the Wi-Fi without Internet? I could not find anything about this in particular in the developer documentation.
非常感谢任何帮助.
推荐答案
为了扩展@ianhanniballake 的答案,我发现使用 ConnectivityManager.setProcessDefaultNetwork() 防止漫游并允许完全 TCP 访问.因此,在 onAvailable() 回调,您可以将应用程序进程绑定到该网络,而不是打开到特定 URL 的连接.
To extend on @ianhanniballake's answer, I've found that binding the network using ConnectivityManager.setProcessDefaultNetwork() prevents roaming and allows for full TCP access. Thus, within the onAvailable() callback you could bind the application process to that network rather than opening a connection to a particular URL.
ConnectivityManager connection_manager =
(ConnectivityManager) activity.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
connection_manager.registerNetworkCallback(request.build(), new NetworkCallback() {
@Override
public void onAvailable(Network network) {
ConnectivityManager.setProcessDefaultNetwork(network);
}
}
从 API 级别 23 开始:请使用以下 OnAvailable 方法:
As of API Level 23: Please use the following OnAvailable Method:
@Override
public void onAvailable(Network network) {
connection_manager.bindProcessToNetwork(network);
}
这篇关于Wi-Fi无法上网时Android Lollipop默认使用Mobile Data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!