4

I'm trying to add some connection state awareness to my app. Using examples here and google's documentation iv'e come up with a receiver that shows an alert correctly when the connection status changed, but also unwantedly shows it whenever the activity is created.

the ConnectStatusReceiver:

package com.zivtaller.placefinder.receivers;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.util.Log;
 import android.widget.Toast;

  public class ConnectStatusReceiver extends BroadcastReceiver {

private String TAG = "netReceiver";

public ConnectStatusReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
    if (isConnected) {
        Log.d(TAG, "Data connected");
        Toast.makeText(context, "Data connected", Toast.LENGTH_SHORT)
                .show();
    } else {
        Log.d(TAG, "Data not connected");
        Toast.makeText(context, "Data not connected", Toast.LENGTH_SHORT)
                .show();
    }

}

}

its initialization and registration in activity's onResume():

        netReceiver = new ConnectStatusReceiver();
    IntentFilter netFilter = new IntentFilter();
    netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(netReceiver, netFilter);
Kepedizer
  • 826
  • 2
  • 10
  • 24

3 Answers3

6

The system is likely generating sticky broadcast android.net.conn.CONNECTIVITY_CHANGE intents. Sticky broadcasts stay around after they are sent. Whenever you register a BroadcastReceiver to listen for broadcasts that are sticky, the most recently broadcasted intent will be passed to the receiver immediately when registerReceiver() is called.

An easy way to test whether or not the android.net.conn.CONNECTIVITY_CHANGE broadcast is sticky would be to check the return value of your registerReceiver() call in onResume()

netReceiver = new ConnectStatusReceiver();
IntentFilter netFilter = new IntentFilter();
netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
Intent intent = registerReceiver(netReceiver, netFilter);
if (intent != null) {
    // broadcast is sticky
}

If you are able to determine that the broadcasts are indeed sticky, then what you are experiencing is intended behavior.

See what is the difference between sendStickyBroadcast and sendBroadcast in Android.

Community
  • 1
  • 1
Bryan Dunlap
  • 1,156
  • 10
  • 7
  • stickiness seems to have been the issue. had it resolved by saving the status on every change and using the saved status when initiating and registering the listener at onResume. – Kepedizer Mar 08 '14 at 13:48
3

Please do ,

unregisterReceiver(netReceiver);

On your onPause() method.

László Papp
  • 51,870
  • 39
  • 111
  • 135
3

As answered Bryan Dunlap you are dealing with sticky intent. To check if intent that you have received is sticky, you should call isInitialStickyBroadcast() in onReceive()

https://stackoverflow.com/a/11825203/1735100

Volo
  • 28,673
  • 12
  • 97
  • 125
Denys Vasylenko
  • 2,135
  • 18
  • 20