在Android上连接WIFI后如何通过移动网络保持连接? [英] How to stay connected through mobile network after WIFI is connected on Android?

查看:552
本文介绍了在Android上连接WIFI后如何通过移动网络保持连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当通过3G(移动)连接从远程服务器流式传输音频,并且当WIFI断开或关闭时,一旦激活并连接WIFI,就会断开通过3G的连接.

I noticed that while streaming audio from a remote server through 3G (mobile) connection and while the WIFI is disconnected or OFF, as soon as WIFI is activated and connected, connection through 3G is dropped.

即使现在也已连接WIFI,我也希望该应用程序继续使用3G.我想这样做以保持连续性. (用户可以选择加入/退出此行为).

I want the app keep using 3G even if WIFI is connected too now. I want to do this to keep continuity. (User may opt-in/out to/from this behaviour).

有特殊的标志,锁等吗?

Is there a special flag, lock, etc.. For this purpose?

推荐答案

在Android 5.0(Lollipop)之前的设备上这是不可能的.操作系统一次只能保持一个接口,应用程序对此选项没有任何控制.

This isn't possible on devices before Android 5.0 (Lollipop). The OS only keeps one interface up at a time, and applications don't have any control over this choice.

在运行Android 5.0或更高版本的设备上,您可以使用新的多网络API来选择要用于网络流量的接口.

On devices running Android 5.0 or newer, you can use the new multi-networking APIs to pick which interface you want to use for network traffic.

以下是执行此操作的步骤,来自 Android 5.0更新日志:

Here's the steps to do this, from the Android 5.0 changelog:

要从您的应用中动态选择并连接到网络,请按照以下步骤操作:

To select and connect to a network dynamically from your app, follow these steps:

  1. 创建一个ConnectivityManager.
  2. 使用NetworkRequest.Builder类创建NetworkRequest对象,并指定您的应用感兴趣的网络功能和传输类型.
  3. 要扫描合适的网络,请调用requestNetwork()registerNetworkCallback(),并传入NetworkRequest对象和ConnectivityManager.NetworkCallback的实现.如果要在检测到合适的网络后主动切换到合适的网络,请使用requestNetwork()方法;要仅接收有关扫描网络的通知而不进行主动切换,请使用registerNetworkCallback()方法.
  1. Create a ConnectivityManager.
  2. Use the NetworkRequest.Builder class to create an NetworkRequest object and specify the network features and transport type your app is interested in.
  3. To scan for suitable networks, call requestNetwork() or registerNetworkCallback(), and pass in the NetworkRequest object and an implementation of ConnectivityManager.NetworkCallback. Use the requestNetwork() method if you want to actively switch to a suitable network once it’s detected; to receive only notifications for scanned networks without actively switching, use the registerNetworkCallback() method instead.

当系统检测到合适的网络时,它会连接到网络并调用onAvailable()回调.您可以使用回调中的Network对象获取有关网络的其他信息,或将流量定向为使用所选网络.

When the system detects a suitable network, it connects to the network and invokes the onAvailable() callback. You can use the Network object from the callback to get additional information about the network, or to direct traffic to use the selected network.

具体来说,如果您想通过3G/LTE强制流量,即使存在WiFi信号,您也可以使用以下方式:

Specifically, if you want to force your traffic over 3G/LTE, even if there's a WiFi signal present, you'd use something like this:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
                          Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder req = new NetworkRequest.Builder();
req.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(req.build(), new ConnectivityManager.NetworkCallback() {

    @Override
    public void onAvailable(Network network) {
        // If you want to use a raw socket...
        network.bindSocket(...);
        // Or if you want a managed URL connection...
        URLConnection conn = network.openConnection(...);
    }

    // Be sure to override other options in NetworkCallback() too...

}

这篇关于在Android上连接WIFI后如何通过移动网络保持连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆