Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

connection - Android Lollipop defaults to Mobile Data when Wi-Fi has not Internet access?

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?

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.

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

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.

Any help is really appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

As of API Level 23: Please use the following OnAvailable Method:

@Override
public void onAvailable(Network network) {
    connection_manager.bindProcessToNetwork(network);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...