Wi-Fi无法上网时Android Lollipop默认使用Mobile Data?

Android Lollipop defaults to Mobile Data when Wi-Fi has not Internet access?(Wi-Fi无法上网时Android Lollipop默认使用Mobile Data?)
本文介绍了Wi-Fi无法上网时Android Lollipop默认使用Mobile Data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)