Question

I am having a strange issue. I have a very simple piece of code that is meant to trigger whenever someone connects to a wifi access point.

IntentFilter ConnectedFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
getApplicationContext().registerReceiver(ConnectedToAPReceiver, ConnectedFilter);

    private BroadcastReceiver ConnectedToAPReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            final String action = intent.getAction();

            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) 
            {
                ConnectivityManager connManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo Wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
              if (Wifi.isConnected()) 
              {
                      // If we get here, it means that the user has just connected to wifi. 
              }
            }
        }
};

This code works very well for what I need it to do. However I am getting strange occurrence, that when the back, or home button is pressed the broadcast receiver is also triggered. Also, loading up the App triggers this as well.

Does anyone know why pressing these buttons would be viewed by Android as causing a connectivity change? And does anyone have any idea's how to easily distinguish between these button presses and a valid connectivity change.


This is part of a solution thanks to some work.

Firstly I have two classes, an activity class and a service class. In my activity class, I set a variable in the service class using a broadcast receiver whenever the onPause and the onDestroy are called. (These get triggered when the user presses these buttons). The receiver in the service class picks up this intent and sets the public variable.

Then in the activity classes onResume I check this variable, and send another intent to the service to reset the variable to it's default state. Here I also set a local flag. When I'm doing any processing I simply check that flag beforehand and handle it appropriately.

Was it helpful?

Solution

I can confirm that behavior in apps of mine as well. I dont believe it is the act of pressing back or home that is really causing this, rather that whatever is now being resumed as a result of the back or home (widgets in the home case, I suppose) happens to use a network connection, requested or started using a connection and that has the affect of triggering your Broadcast receiver.

If you didnt want to respond to these false positives, you could do a bit more processing of the Intent your BroadcastReceiver gets and possibly keep track of the wifi connection state within your receiver so you dont take some action when you get another CONNECTIVITY_ACTION but know that wifi is already connected.

The documentation around some of these arent super clear but here are some additional things you can inspect about the intent that might help you decide whether or not you need to do something:

// true when there is no connectivity whatsoever
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

// lots of extra info & detailed state about the connection
// see http://developer.android.com/reference/android/net/NetworkInfo.html   
NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

NetworkInfo otherNetworkInfo = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top