8

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

xain
  • 11,420
  • 17
  • 70
  • 116

1 Answers1

26

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();
Mr_and_Mrs_D
  • 27,070
  • 30
  • 156
  • 325
Zelimir
  • 10,848
  • 5
  • 46
  • 45
  • Are you sure about it? Does not receive broadcasts at all or what? – Zelimir Sep 28 '12 at 10:37
  • it receives broadcasts , but noConnectivity returns false all the time. – meh Oct 01 '12 at 18:31
  • Interesting. Please give mi more info. Which phone, OS version, WiFi or GPRS? How do you cause no connectivity condition. Thanks. – Zelimir Oct 02 '12 at 08:42
  • I was testing both WIFI and GPRS and I just switched from airplane mode on and off. my OS version is 2.2, and the device is Samsung Galaxy S1. and the way I have solved it was : connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivity.getActiveNetworkInfo(); and checking if the activeNetInfo was not null. – meh Oct 02 '12 at 10:29
  • That is correct way to obtain current state (initial value). But to monitor changes you need to register for broadcast reception, does not make sense to perform periodic checking every now and then. Still puzzled why it does not work for you. Proved this to work on 7 different Android phones, from Android 2.1 up. Never observed issue. – Zelimir Oct 02 '12 at 14:17
  • I'm doing the check I have mentioned inside the receiver and it works fine for me. – meh Oct 03 '12 at 08:18
  • Makes sense. Looks like system does not send EXTRA_NO_CONNECTIVITY, and for that reason you always get false (set as default in my code). You may check if intent coming from the system has extra EXTRA_NO_CONNECTIVITY. Waterproof solution would be to check if that extra is in intent. If it is, use the approach I described. If it is not the case, apply your solution. – Zelimir Oct 03 '12 at 09:00