Tuesday, 2 October 2018

Is there a way to check if my current wifi connected is different from previous connected wifi?

I want to detect if my wifi connection has changed(if it has changed to cellular or some other wifi but not the same) after I return to the app from background. Is there any way to do this? I tried the below code but it doesn't work, it keeps firing up every single time i return to the app even if the same wifi stays connected.

 IntentFilter networkIntent = new IntentFilter();
        networkIntent.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkIntent.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        registerReceiver(wifichangereceiver, networkIntent);

Below is my receiver:

public final BroadcastReceiver wifichangereceiver = new BroadcastReceiver() {


        @Override
        public void onReceive(final Context context, final Intent intent) {
            final ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            final android.net.NetworkInfo wifi = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (wifi.isConnected()) {
                hasWifiChanged = true;
              } else {
                hasWifiChanged = false;
              }
}



from Is there a way to check if my current wifi connected is different from previous connected wifi?

No comments:

Post a Comment