narb narb - 1 month ago 16
Android Question

Why do I have several states change for a BroadcastReceiver On Wifi changes

I want to detect wifi on/off changes. My code below works fine but I go through several iterations of the broadcastreceiver.
For example, if I turn on wifi in the settings, I obtain 8 times false and 2 times true as the value of the boolean coonected.
Is it normal?

public class MainActivity extends AppCompatActivity {
TextView wifistate = null;

BroadcastReceiver broadcastReceiver;
WifiManager wifi;
Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = getApplicationContext();

wifistate = (TextView) findViewById(R.id.wifistate);
wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

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

if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if (connected) {
Log.i("RCVR", "True");
wifistate.setText("Connected");
} else {
Log.i("RCVR", "False");
wifistate.setText("Disconnected");
}

}
}
};

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);


}

}


and here are the iterations. The end result is correct.

enter image description here

Answer

I've seen this before. So Here's what I do.

Note: I'm using a different action for the receiver.

Here's your solution:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(broadcastReceiver, intentFilter);

Then the receiver:

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        Bundle extras = intent.getExtras();

        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = (NetworkInfo) extras.get(ConnectivityManager.EXTRA_NETWORK_INFO);
            boolean connected = info.isConnected();
            if (connected && isWifiConnected()) {
                Log.i("RCVR", "True");
                wifistate.setText("Connected");
            } else {
                Log.i("RCVR", "False");
                wifistate.setText("Disconnected");
            }
        }
    }
};

Check if the connected if WiFi or not:

public static boolean isWifiConnected() {
    ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI);
}

This way you are checking if the connection is available and checking if the connection is WiFi.
It seems to work for me. Hope it helps.

Comments