Submitting events
Now with events defined, we will create the BroadcastListener that is going to receive Intents from the Android OS when any network connectivity changes (Wi-Fi, Mobile, …) occur on the device, and submits the events in the Bus when the mobile connectivity has changed:
public class MobileNetworkListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Retrieve the NetworkInfo from the received Intent
NetworkInfo info =(NetworkInfo)intent.
getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if ( isMobileNetwork(context, info) && !info.isConnected()) {
// Publish an mobile network disconnected Event
EventBus.getDefault().post(
new MobileNetDisconnectedEvent());
} else if ( isMobileNetwork(context, info) &&
info.isConnected()) {
// Publish an mobile network connected Event
EventBus.getDefault().post(
new MobileNetConnectedEvent...