检测3G或Wifinetworking恢复

是否有可能实现一个PhoneStateListener(或任何其他机制)来检测3G或Wifinetworking连接何时恢复?

我在API的总结中看到了LISTEN_DATA_CONNECTION_STATE和LISTEN_DATA_ACTIVITY(蜂窝)。 这是否意味着3G?

谢谢

Solutions Collecting From Web of "检测3G或Wifinetworking恢复"

更好的方法是使用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();