Detect 3G or Wifi Network restoration
Asked Answered
D

1

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

Daffodil answered 21/12, 2010 at 20:24 Comment(0)
B
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();
Burkes answered 21/12, 2010 at 21:32 Comment(7)
Are you sure about it? Does not receive broadcasts at all or what?Burkes
it receives broadcasts , but noConnectivity returns false all the time.Rheta
Interesting. Please give mi more info. Which phone, OS version, WiFi or GPRS? How do you cause no connectivity condition. Thanks.Burkes
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.Rheta
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.Burkes
I'm doing the check I have mentioned inside the receiver and it works fine for me.Rheta
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.Burkes

© 2022 - 2024 — McMap. All rights reserved.