I would receive notifications when signal strength changes. I tried to create the following method and call it in the onCreate():
private void initializeWiFi
Just to drop my solution:
in my App I have
@Override
protected void onResume() {
super.onResume();
networkStateReceiver = new NetworkStateReceiver(this);
this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.wifi.SCAN_RESULTS"));
networkStateReceiver.registerListener(this);
}
And the onReceive in my Receiver looks like:
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_ETHERNET:
this.networkStateMessage = new NetworkStateMessage(networkInfo);
break;
case ConnectivityManager.TYPE_WIFI:
this.networkStateMessage = new NetworkStateMessage(networkInfo, wifiManager.getConnectionInfo());
worker.schedule(new Runnable() {
@Override
public void run() { wifiManager.startScan(); }
}, 5, TimeUnit.SECONDS);
break;
default:
this.networkStateMessage = null;
break;
}
}
notifyStateToAll(this.networkStateMessage);
}
The idea is the to startScan() only every 5 seconds if the wifi is the active network.