检测3G或Wifi网络恢复

发布于 2024-10-08 20:00:27 字数 164 浏览 5 评论 0原文

是否可以实现 PhoneStateListener(或任何其他机制)来检测 3G 或 Wifi 网络连接何时恢复?

我在 API 摘要中看到 LISTEN_DATA_CONNECTION_STATE 和 LISTEN_DATA_ACTIVITY 都说(蜂窝)。是指只有3G吗?

谢谢

Is it possible to implement a PhoneStateListener(or any other mechanism) to detect when either the 3G or Wifi network connection is restored ?

I see both LISTEN_DATA_CONNECTION_STATE and LISTEN_DATA_ACTIVITY say (cellular) in the API's summary. Does it mean 3G only ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-10-15 20:00:27

更好的方法是使用 android.net.ConnectivityManager 类。注册接收器并监听广播。

private class ConnectionMonitor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            return;
        }
        boolean noConnectivity = intent.getBooleanExtra(
            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo aNetworkInfo = (NetworkInfo) intent
            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (!noConnectivity) {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // Handle connected case
            }
        } else {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // Handle disconnected case
            }
        }
    }
}

private synchronized void startMonitoringConnection() {
    IntentFilter aFilter = new IntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(mConnectionReceiver, aFilter);
}
private synchronized void stopMonitoringConnection() {
    unregisterReceiver(mConnectionReceiver);
}

在哪里

mConnectionReceiver = new ConnectionMonitor();

Better approach would be to use android.net.ConnectivityManager class. Register the receiver and monitor broadcasts.

private class ConnectionMonitor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            return;
        }
        boolean noConnectivity = intent.getBooleanExtra(
            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        NetworkInfo aNetworkInfo = (NetworkInfo) intent
            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (!noConnectivity) {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // Handle connected case
            }
        } else {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI)) {
                // Handle disconnected case
            }
        }
    }
}

private synchronized void startMonitoringConnection() {
    IntentFilter aFilter = new IntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(mConnectionReceiver, aFilter);
}
private synchronized void stopMonitoringConnection() {
    unregisterReceiver(mConnectionReceiver);
}

where

mConnectionReceiver = new ConnectionMonitor();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文