By   June 17, 2014

This post is amongst the series of post for discussion about development of an App in Android Platform. We have been developing an app, named GitHub Contributions as an example. In each post, we keep adding some new feature to the app, which adds to our knowledge about android.

In this post, we are going to see how we can add offline support to our app. So if a user has launched our app and there is no internet connection available, we cannot download user profile’s data from github.com. In this case, we can show any previous data downloaded for the specified user. If we are running the app for the first time for the user, then we can just show a Not-Connected message on the screen. As soon as the user connects to the network, we can attempt to download the information and show the pretty view.

What to display when not connected?

Developing Offline support for your app, also includes how to notify user of no connectivity. Since the regular view needs the actual data to be shown, it doesn’t really make sense to still load the whole view with empty data. I think the better option is to display a simpler view with details about inactivity and if there is anything a user can do to fix it.

layoutresourcefile_create

In order to keep things simple here, we are just showing a message to the user. It should say “Not Connected!”.

Checking Network Connectivity

We can use ConnectivityManager to determine network state. In the code below, we are checking if the device is connected to a network. We can use Context to get the ConnectivityManager object from system. It is registered as a system service CONNECTIVITY_SERVICE.

Since we are periodically downloading user data after every 3 hours. We need to register for connectivity changes to allow application state based on the connectivity. A BroadcastReceiver can be registered in a Context for the specified intent. Here is the definition of ConnectivityReceiver.

When the connectivity changes for the device, onReceive() would be called. Here we are using the same connectivity service to determine the current state of connectivity and updating the Activity about this.

Simulator & Checking Network Connectivity

If you are debugging your app in a simulator then the NetworkInfo would always consider the phone to be connected. In order to test the connectivity logic, we can put the phone in Airplane mode. In this case, the layout definition for “not connected” state would appear as follows:

NotConnectedDisplayed

Permission to Access Network State

Now if we run the application as is, we get the following exception when we try to get the state of network.

SecurityException

Actually we need to request permissions for accessing network state in AndroidManifest.xml. We can define it as following:

App Startup

When the app starts up, onCreate() is called. Here we need to decide what information we need to display to the user. It is also the the usual place to set the layout resource definition xml to be used for the activity. In our case, we can determine, if the user is connected to the network, if so, we can use the older logic to download user information only in the case that the user information is not current i.e. downloaded less than 3 hours ago.

We have also updated to loadUserDetails() to relax the 3 hours requirement for using the user’s data from persistence. If the user is not connected, we can show the older data. On the other hand, we wouldn’t use older data if the user is connected and would download the fresh data at app startup.

Changing modes with Connectivity changes

Since our app can be used in online and offline modes, we need to update the app state when connectivity state changes.

Get it from Git

AndroidApp_GitHubDownload