24

我正在构建一个 VoIP 应用程序。在 VoIP 呼叫期间,当用户在 WiFi 和移动数据之间切换时,我在处理该场景时遇到了问题。

在我的呼叫屏幕活动中,我已注册接收器,这有助于我获得有关网络更改方案的通知。

这是我用来检测 onRecieve 方法中网络变化的代码。conn_name是保存先前连接名称的私有类级别变量。

ConnectivityManager connectivity_mgr = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo net_info = connectivity_mgr.getActiveNetworkInfo();

if (net_info != null && net_info.isConnectedOrConnecting() && !conn_name.equalsIgnoreCase("")) {
    new_con = net_info.getExtraInfo();
    if (new_con != null && !new_con.equalsIgnoreCase(conn_name))
        network_changed = true;
    conn_name = (new_con == null) ? "" : new_con;
    connectionStatus ="connected";
} else {
    if (net_info != null && conn_name.equalsIgnoreCase("")) {
        conn_name = net_info.getExtraInfo();
        connectionStatus ="connected";
        network_changed = true;
    } else if(!new_con.equals(conn_name)) {
        conn_name = "";
        connectionStatus ="disconnected";
        network_changed = true;
    }
}

所以使用上述方法我能够检测到网络变化。但是当我连接到 WiFi 时会发生一件奇怪的事情。当我的应用程序最初启动时,它与移动数据连接。当用户进入他已知的 WiFi 区域时,他会连接到他已知的 WiFi。由于始终选择 WiFi 作为默认路由,Android 切换到 WiFi 并且我收到 WiFi 已打开的网络通知。

所以我将我的应用程序 IP 地址更新为 WiFi IP 地址,所以这里没有问题。但是移动数据仍然同时连接,但getActiveNetworkInfo()告诉我,即使我很早就连接到移动数据,我也清楚地连接了 WiFi。

所以问题是当用户关闭 WiFi 按钮时,移动数据仍然连接,但我仍然收到 WiFi 关闭的通知。即使我的手机仍连接到移动数据,这表明我的网络已断开连接。

但一秒钟后,我收到移动数据已连接的通知。但是一旦我收到网络断开连接,我就关闭了我的 VoIP 通话。因此,当我收到关于 WiFi 已关闭的通知时,如何确定移动数据是否仍处于连接状态。

我尝试了 getActiveNetworkInfo() 但当我收到关闭 WiFi 的通知时它恰好为空。

我已经关注了这个链接:

Android API 调用以确定用户设置“数据已启用”
如何判断“移动网络数据”是启用还是禁用(即使通过 WiFi 连接)?

使用上面的链接,我可以检测到当用户连接 mobiledata 时移动数据按钮已启用。它给了我真实的信息。但是当这种特殊情况发生时,问题就会发生。

现在,当 wifi 被禁用时,我会收到通知,但它显示移动数据被禁用,即使我的移动数据已启用。我无法处理这种情况,因为我在收到已断开连接的通知时会断开呼叫。

4

3 回答 3

33

您可以使用以下 API ConnectivityManager:特别是在您感兴趣的用例中registerDefaultNetworkCallback()


    public class TestActivity extends AppCompatActivity {

        private ConnectivityManager manager;
        private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                // this ternary operation is not quite true, because non-metered doesn't yet mean, that it's wifi
                // nevertheless, for simplicity let's assume that's true
                Log.i("vvv", "connected to " + (manager.isActiveNetworkMetered() ? "LTE" : "WIFI"));
            }

            @Override
            public void onLost(Network network) {
                super.onLost(network);
                Log.i("vvv", "losing active connection");
            }
        };

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            manager.registerDefaultNetworkCallback(networkCallback);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            manager.unregisterNetworkCallback(networkCallback);
        }
    }

我的设备在大约半秒内连接到 LTE。

在此处输入图像描述

这意味着,当 WIFI 断开连接时,您无法事先知道设备最终是否会连接到 LTE。因此,您可以采用以下方法:在处理程序上发布一个动作以在一秒钟内发生,并在此动作中取消调用。如果连接很快出现 - 取消安排之前发布的操作。如果您最终在Runnable代码中,则连接没有很快建立,这意味着您应该结束通话。


    public class TestActivity extends AppCompatActivity {

        private ConnectivityManager manager;

        private final Handler handler = new Handler();
        private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
                Log.i("vvv", "connected to " + (manager.isActiveNetworkMetered() ? "LTE" : "WIFI"));

                // we've got a connection, remove callbacks (if we have posted any)
                handler.removeCallbacks(endCall);
            }

            @Override
            public void onLost(Network network) {
                super.onLost(network);
                Log.i("vvv", "losing active connection");

                // Schedule an event to take place in a second
                handler.postDelayed(endCall, 1000);
            }
        };

        private final Runnable endCall = new Runnable() {
            @Override
            public void run() {
                // if execution has reached here - feel free to cancel the call
                // because no connection was established in a second
            }
        };

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            manager.registerDefaultNetworkCallback(networkCallback);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            manager.unregisterNetworkCallback(networkCallback);
            handler.removeCallbacks(endCall);
        }
    }

该方法的缺点是,它registerDefaultNetworkCallback()从 API 24 开始可用。两者都不存在替代方案ConnectivityManagerCompat。相反,您可以使用registerNetworkCallback()API 21 提供的。

于 2018-01-18T18:06:16.333 回答
4

我使用 RxJava 的实现

class ConnectivityMonitor : ConnectivityManager.NetworkCallback() {
    var networkTimeout: Disposable? = null

    override fun onAvailable(network: Network?) {
        super.onAvailable(network)
        Timber.d("Network available")
        networkTimeout?.dispose()
    }

    override fun onLosing(network: Network?, maxMsToLive: Int) {
        super.onLosing(network, maxMsToLive)
        Timber.d("onLosing")
    }

    override fun onLost(network: Network?) {
        super.onLost(network)
        Timber.d("onLost")

        networkTimeout = Single.timer(5, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { _ -> Timber.d("Network lost") }
    }

    override fun onUnavailable() {
        super.onUnavailable()
        Timber.d("Network unavailable")
    }
}

监听器设置:

    private fun setupListeners() {
        // connection listener
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            connectivityManager.registerDefaultNetworkCallback(connectivityMonitor)
        } else {
            val builder = NetworkRequest.Builder()
            connectivityManager.registerNetworkCallback(builder.build(), connectivityMonitor)
        }
    }

定时器/一次性的使用允许切换连接类型之间的延迟。

于 2019-04-10T17:04:28.493 回答
3

您可以使用BroadcastReceiver和注册NETWORK_STATE_CHANGED_ACTION& WIFI_STATE_CHANGED_ACTION

private boolean isConnected;

final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || intent.getAction() == null)
            return;
        switch (intent.getAction()){
            case WifiManager.NETWORK_STATE_CHANGED_ACTION :
            case WifiManager.WIFI_STATE_CHANGED_ACTION :
                if (!isConnected && isOnline(BaseActivity.this)) {
                    isConnected = true;
                    // do stuff when connected
                    Log.i("Network status: ","Connected");
                }else{
                    isConnected = isOnline(BaseActivity.this);
                    Log.i("Network status: ","Disconnected");
                }
                break;
        }
    }
};



@Override
protected void onCreate(Bundle savedInstanceState) {
    isConnected = isOnline(this);
    final IntentFilter filters = new IntentFilter();
    filters.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    filters.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    registerReceiver(broadcastReceiver, filters);
}


public static boolean isOnline(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm != null
            ? cm.getActiveNetworkInfo()
            : null;
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

更新不要忘记取消注册接收器BroadcastReceiver onDestroy

@Override
protected void onDestroy() {
    unregisterReceiver(broadcastReceiver);
    super.onDestroy();
} 
于 2018-01-24T06:47:48.100 回答