Try to catch WifiManager.WIFI_STATE_ENABLED while listening to the WIFI_STATE_CHANGED event - this state will appear after all connection procedures have been completed, so the ip gateway should be configured correctly at this point.
this should go to your onResume function:
IntentFilter filter = new IntentFilter(); filter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); this.registerReceiver(networkStateListener, filter);
this - to onPause
this.unregisterReceiver(networkStateListener);
and this is the receiver itself
BroadcastReceiver networkStateListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(BroadcastReceiver.class.getSimpleName(), "action: " + intent.getAction()); int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,-1); isNetworkAvailable =state == WifiManager.WIFI_STATE_ENABLED;
- I have not tested this solution, it is just a suggestion, so if it doesnβt work, please let me know.
source share