A BroadcastReceiver in your Activity

In developing an application which content is all based on a RESTfull api, I needed a solid listener for the network connection of the used device. No internet equals no content, so the app had to tell the user about that and more importantly – react if the users decides to activate the connection.

It’s done in a process that took a while to grasp and build in my app, so let’s get into a bit more detail. Network connectivity is a system wide event, managed outside your app itself, therefore, if you want to react to it you need to implement a BroadcastReceiver. A BroadcastReceiver is a class that receives calls from Android system, system-to-app communication if you like. The BroadcastReceiver is often placed in the manifest, however, I needed a more hands-on approach for my app functionality. Basically, the BroadcastReceiver has to call a method in my AndroidActivity class as soon as a network connection is available.

In most of the applications Activity-classes an AsyncTask is used to communicatie with the API. If you write it down, it’s really quite simpel:

Is there a network connection?

– Yes; talk with the API.

– No; inform the user and wait for the connection. Then talk to the API.

Communication with the API is done by an AsyncTask inner class which is housed in an abstract baseclass as I need it often in the activities.

In a bit more detail:

        final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
            Log.i("BroadcastReceiver", "Network " + networkInfo.getTypeName() + " available!");
            startAsyncTask();
        }
    }
}

};

} else { // No connection yet // inform you user with a Toast or the preferred Snackbar message // Register the BroadcastReceiver to fetch your data later registerReceiver(networkStateReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); }

unregisterReceiver(networkStateReceiver);